HashMap和Hashtable的区别(网上流传版本的修正版)
尽信书不如无书,我今天在看网上的一些资料的时候发现一遍篇《HashMap和Hashtable的区别》的文章,随手就在Eclipse里实验了一下,结果发现很多原来文章中的错误,现在把这个问题修改好以后贴在这里,希望对大家的学习也有帮助。
HashMap和Hashtable的区别。
错误说法:
<!--[if !supportLists]-->1. <!--[endif]-->HashTable不允许null值(key和value都不可以),HashMap允许null值(key和value都可以)。
这句话容易让人误会,到底是怎么个不允许法呢?其实在编译期不会有任何的不一样,会照样执行,只是在运行期的时候Hashtable中设置的话回出现空指针异常
<!--[if !supportLists]-->2. <!--[endif]-->在HashMap中,null可以作为键,这样的键只有一个;可以有一个或多个键所对应的值为null。当get()方法返回null值时,即可以表示 HashMap中没有该键,也可以表示该键所对应的值为null。因此,在HashMap中不能由get()方法来判断HashMap中是否存在某个键, 而应该用containsKey()方法来判断。
不用多说,看下面的程序就可以:
HashMap map = new HashMap();
map.put("Null", null);
map.put(null, "Null");
map.put(null, "Empty");
System.out.println(map.get(null));
System.out.println(map.get("Null"));
System.out.println(map.get("NullThere"));
System.out.println(map.containsKey("Null"));
System.out.println(map.containsKey("NullThere"));
输出结果为:
Empty
null
null
true
false
HashMap
Hashtable
继承,实现
HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable
Hashtable<K,V>
extends Dictionary<K,V>
implements Map<K,V>, Cloneable,Serializable
多线程,同步
未同步的,可以使用Colletcions进行同步
Map Collections.synchronizedMap(Map m)
已经同步过的可以安全使用
对null的处理
HashMap map = new HashMap();
map.put(null, "Null");
map.put("Null", null);
map.containsKey(null);
map.containsValue(null);
以上这5条语句无论在编译期,还是在运行期都是没有错误的.
在HashMap中,null可以作为键,这样的键只有一个;可以有一个或多个键所对应的值为null。当get()方法返回null值时,即可以表示 HashMap中没有该键,也可以表示该键所对应的值为null。因此,在HashMap中不能由get()方法来判断HashMap中是否存在某个键, 而应该用containsKey()方法来判断。
Hashtable table = new Hashtable();
table.put(null, "Null");
table.put("Null", null);
table.contains(null);
table.containsKey(null);
table.containsValue(null);
后面的5句话在编译的时候不会有异常,可在运行的时候会报空指针异常
具体原因可以查看源代码
public synchronized V put(K key, V value) {
// Make sure the value is not null
if (value == null) {
throw new NullPointerException();
}
………….
增长率
void addEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
if (size++ >= threshold)
resize(2 * table.length);
}
protected void rehash() {
int oldCapacity = table.length;
Entry[] oldMap = table;
int newCapacity = oldCapacity * 2 + 1;
Entry[] newMap = new Entry[newCapacity];
modCount++;
threshold = (int)(newCapacity * loadFactor);
table = newMap;
for (int i = oldCapacity ; i-- > 0 ;) {
for (Entry<K,V> old = oldMap[i] ; old != null ; ) {
Entry<K,V> e = old;
old = old.next;
int index = (e.hash & 0x7FFFFFFF) % newCapacity;
e.next = newMap[index];
newMap[index] = e;
}
}
}
哈希值的使用
HashMap重新计算hash值,而且用与代替求模
public boolean containsKey(Object key) {
Object k = maskNull(key);
int hash = hash(k.hashCode());
int i = indexFor(hash, table.length);
Entry e = table[i];
while (e != null) {
if (e.hash == hash && eq(k, e.key))
return true;
e = e.next;
}
return false;
}
HashTable直接使用对象的hashCode,代码是这样的:
public synchronized boolean containsKey(Object key) {
Entry tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
return true;
}
}
return false;
}
分享到:
相关推荐
记得刚毕业那会准备面试,看过不少面试题,里面有个说出HashMap和HashTable不同的题目,我那会面试的时候也遇到不少次这个问题,还隐约记得当时的回答是这样的: HashTable是比较旧的版本;HashTable是线程安全的,...
hashMap和hashTable的区别,大家可以下载学习学习。
初级程序员面试经常问道的问题,HashMap与HashTable区别,希望有帮助
hashmap与hashtable区别 主要是应用于存值的数值对
HashTable不支持空键值对! 而HashMap支持空键值对!
HashMap、HashTable和HashSet是Java中常用的数据结构,它们的底层实现原理以及区别如下:HashMap底层实现原理: HashMap基于哈希表(HashTable)实现,它通过散列算法将键值对映射到数组中。在HashMap中,每个键值对...
Java集合专题总结:HashMap 和 HashTable 源码学习和面试总结
hashmap和hashtable的区别 HashMap和Hashtable都实现了Map接口,但决定用哪一个之前先要弄清楚它们之间的分别。主要的区别有:线程安全性,同步(synchronization),以及速度。 1. HashMap几乎可以等价于Hashtable,...
hashmap和hashtable的区别
经典讲解List和ArrayList和Vector和HashTable和HashMap区别
HashMap和HashTable区别共2页.pdf.zip
hashMap和Hashtable的区别1
(多选)有关hashMap跟hashTable的区别,说法正确的是? A. HashMap和Hashtable都实现了Map接口 B. HashMap是非synchronized,而Hashtable是synchronized C. HashTable使用Enumeration,HashMap使用Iterator D. ...
11.HashMap和HashTable的区别.avi
本文主要介绍HashMap 和 Hashtable的区别,这里整理了相关资料并详细介绍了HashMap 和 Hashtable的区别及其工作原理和使用方法,有需要的朋友可以看一下
HashMap和Hashtable的区别Java开发Java经验技巧共2页.pdf.zip
今天小编就为大家分享一篇关于HashMap和HashTable底层原理以及常见面试题,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
Hashtable和HashMap的区别: 1.Hashtable是Dictionary的子类,HashMap是Map接口的一个实现类; 2.Hashtable中的方法是同步的,而HashMap中的方法在缺省情况下是非同步的。即是说,在多线程应用程序中,不用专门的...
Java面试题11.HashMap和HashTable的区别.mp4
NULL 博文链接:https://qiaolevip.iteye.com/blog/2094447