java中Hashtable和HashMap的區(qū)別分析
1、Hashtable是Dictionary的子類,
public class Hashtable<K,V>
extends Dictionary<K,V>
implements Map<K,V>, Cloneable, java.io.Serializable
HashMap:
public class HashMap<K,V>
extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable
HashMap和Hashtable都是Map接口的一個(gè)實(shí)現(xiàn)類;
2、Hashtable中的方法是同步的(),而HashMap中的方法在默認(rèn)情況下不是同步的。即是說,在多線程應(yīng)用程序中,不用專門的操作就安全地可以使用Hashtable了;而對(duì)于HashMap,則需要額外的同步機(jī)制。但HashMap的同步問題可通過Collections的一個(gè)靜態(tài)方法得到解決:
public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m)
這個(gè)方法返回一個(gè)同步的Map,也就是說返回的Map是線程安全的。需要注意的是,對(duì)返回的map進(jìn)行迭代時(shí),必須手動(dòng)在返回的map上進(jìn)行同步,否則將會(huì)導(dǎo)致不確定的行為:
Map m = Collections.synchronizedMap(new HashMap());
...
Set s = m.keySet(); // Needn't be in synchronized block
...
synchronized(m) { // Synchronizing on m, not s!
Iterator i = s.iterator(); // Must be in synchronized block
while (i.hasNext())
foo(i.next());
}
3.在HashMap中,null可以作為鍵,這樣的鍵只有一個(gè);可以有一個(gè)或多個(gè)鍵所對(duì)應(yīng)的值為null。當(dāng)get()方法返回null值時(shí),即可以表示HashMap中沒有該鍵,也可以表示該鍵所對(duì)應(yīng)的值為null。因此,在HashMap中不能由get()方法來判斷HashMap中是否存在某個(gè)鍵,而應(yīng)該用containsKey()方法來判斷。Hashtable的鍵值不能為null,否則:java.lang.NullPointerException 。
4.HashTable使用Enumeration,HashMap使用Iterator。
以上只是表面的不同,它們的實(shí)現(xiàn)也有很大的不同。
5.HashTable中hash數(shù)組默認(rèn)大小是11,增加的方式是 old*2+1。HashMap中hash數(shù)組的默認(rèn)大小是16,而且一定是2的指數(shù)。
6.哈希值的使用不同,HashTable直接使用對(duì)象的hashCode,代碼是這樣的:
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
而HashMap重新計(jì)算hash值,而且用與代替求模,比如HashMap的put方法:
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
int hash = hash(key.hashCode());
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, key, value, i);
return null;
}
static int hash(int h) {
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
static int indexFor(int h, int length) {
return h & (length-1);
}
相關(guān)文章
詳解Java中List接口底層實(shí)現(xiàn)原理
Java是一種廣泛應(yīng)用的編程語言,被廣泛應(yīng)用于各種平臺(tái)和應(yīng)用領(lǐng)域,List接口是Java中最重要的數(shù)據(jù)結(jié)構(gòu)之一,它為我們提供了一種靈活、高效、可擴(kuò)展的數(shù)據(jù)結(jié)構(gòu),本篇文章將首先介紹Java中List接口的基本特性和使用方法,然后深入研究List接口的底層實(shí)現(xiàn)原理2023-11-11
SpringBoot 嵌入式web容器的啟動(dòng)原理詳解
這篇文章主要介紹了SpringBoot 嵌入式web容器的啟動(dòng)原理詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2021-11-11
SpringBoot繼承LogStash實(shí)現(xiàn)日志收集的方法示例
這篇文章主要介紹了SpringBoot繼承LogStash實(shí)現(xiàn)日志收集的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05
Springboot Cache @CacheEvict 無法模糊刪除的解決方案
這篇文章主要介紹了Springboot Cache @CacheEvict 無法模糊刪除的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot項(xiàng)目部署到騰訊云的實(shí)現(xiàn)步驟
本文主要介紹了SpringBoot項(xiàng)目部署到騰訊云的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01

