hashmap and hashtable 区别
来源:互联网 发布:英克软件售后 编辑:程序博客网 时间:2024/05/21 04:16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//HashMap的源码
public
class
HashMap<K,V>
extends
AbstractMap<K,V>
implements
Map<K,V>, Cloneable, Serializable{
//HashMap的put方法,没有同步
public
V put(K key, V value){
...
if
(key ==
null
)
return
putForNullKey(value);
...
}
//以下是HashMap中的方法,注意,没有contains方法
public
boolean
containsKey(Object key) {...}
public
boolean
containsValue(Object value){...}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Hashtable的源码
public
class
Hashtable<K,V>
extends
Dictionary<K,V>
implements
Map<K,V>, Cloneable, java.io.Serializable{
//Hashtable的put方法
//当然,Hashtable的其他方法,如get,size,remove等方法,
//都加了synchronized关键词同步操作
public
synchronized
V put(K key, V value){
...
if
(value ==
null
) {
throw
new
NullPointerException();
}
...
}
//以下是Hashtable的方法
public
synchronized
boolean
contains(Object value);
public
synchronized
boolean
containsKey(Object key);
public
boolean
containsValue(Object value);
}
1、继承不同。public class Hashtable extends Dictionary implements Map
public class HashMap extends AbstractMap implements Map
2、Hashtable 中的方法是同步的,而HashMap中的方法在缺省情况下是非同步的。在多线程并发的环境下,可以直接使用Hashtable,但是要使用HashMap的话就要自己增加同步处理了。
3、Hashtable中,key和value都不允许出现null值。
在HashMap中,null可以作为键,这样的键只有一个;可以有一个或多个键所对应的值为null。当get()方法返回null值时,即可以表示 HashMap中没有该键,也可以表示该键所对应的值为null。因此,在HashMap中不能由get()方法来判断HashMap中是否存在某个键, 而应该用containsKey()方法来判断。
4、两个遍历方式的内部实现上不同。
Hashtable、HashMap都使用了 Iterator。而由于历史原因,Hashtable还使用了Enumeration的方式 。
5、哈希值的使用不同,HashTable直接使用对象的hashCode。而HashMap重新计算hash值。
6、Hashtable和HashMap它们两个内部实现方式的数组的初始大小和扩容的方式。HashTable中hash数组默认大小是11,增加的方式是 old*2+1。HashMap中hash数组的默认大小是16,而且一定是2的指数
阅读全文
0 0
- hashmap and hashtable 区别
- HashMap与HashTable区别(Differences between HashMap and Hashtable)
- Hashtable and HashMap
- HashTable and HashMap
- HashMap, HashTable and HashSet
- HashMap和Hashtable区别
- HashMap, HashTable, HashSet区别
- HashMap Hashtable区别
- hashtable和hashmap区别
- Hashtable HashMap HashSet 区别
- HashMap 与HashTable 区别
- HashMap,HashTable,HashSet区别
- HashMap Hashtable区别
- HashMap,HashTable,HashSet区别
- HashMap与HashTable区别
- Hashtable和HashMap区别
- Hashtable与HashMap区别
- HashMap HashTable区别
- js中怎样把ASCII码中的字符与十进制的数相互转换-学习笔记
- O2O分析报告
- shell programmable
- 汇编指令集
- spring 注解之@interface
- hashmap and hashtable 区别
- C++笔记——函数模板
- HTML总结
- 汇编语言的七种寻址方式
- 桥接模式(bridge)c++版
- 2017.10.10 第一天总结
- vue、angular、react前端三大框架的区别
- oracle学习总结(三)
- uniVocity-parsers:速度最快功能最全的CSV开发库