Hashtable是HashMap的線程安全版本,它的實作和HashMap實作基本一緻,除了它不能包含null值的key和value,并且它在計算hash值和數組索引值的方式要稍微簡單一些。
Hashtable線程安全實作方式是将所有方法都标記成synchronized,但這樣加鎖的粒度大,容易引起一些性能問題,是以目使用java.concurrent.ConcurrentHashMap類性能更佳
在JDK1.7之後,HashMap和HashTable的哈希函數都一樣了,但由hash值轉換成表索引的方式不一樣:
- HashMap使用&位操作 : h & (length-1);
- HashTable使用取餘操作 : (hash & 0x7FFFFFFF) % tab.length;
HashMap源碼如下:
package java.util;
import java.io.*;
public class HashMap<K,V>
extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable
{
/*
HashMap 的執行個體有兩個參數影響其性能:初始容量 和加載因子。
容量是哈希表中桶的數量,初始容量隻是哈希表在建立時的容量。
加載因子是哈希表在其容量自動增加之前可以達到多滿的一種尺度。
當哈希表中的條目數超出了加載因子與目前容量的乘積時,
則要對該哈希表進行 rehash 操作(即重建内部資料結構),
進而哈希表将具有大約兩倍的桶數。
加載因子預設值為0.75,預設哈希表容量為16
*/
//初始化容量16 hashMap的容量必須是2的指數倍 Hashtable是11
static final int DEFAULT_INITIAL_CAPACITY = << ;
//最大容量2的30次方
static final int MAXIMUM_CAPACITY = << ;
//預設加載因子預設的平衡因子為0.75,這是權衡了時間複雜度與空間複雜度之後的最好取值(JDK說是最好的),過高的因子會降低存儲空間但是查找(lookup,包括HashMap中的put與get方法)的時間就會增加。
static final float DEFAULT_LOAD_FACTOR = f;
//用來存儲鍵值對的Entry數組,用于設定剛剛初始化的HashMap對象,用來減少存儲空間
static final Entry<?,?>[] EMPTY_TABLE = {};
//大小必須是2的倍數
transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;
//存儲的鍵值對的數目
transient int size;
//門檻值,當size超過threshold時,table将會擴容.
//threshold = capacity * loadFactor
int threshold;
//加載因子
final float loadFactor;
//修改次數,用于檢查線程是否同步
transient int modCount;
//預設的閥值
static final int ALTERNATIVE_HASHING_THRESHOLD_DEFAULT = Integer.MAX_VALUE;
private static class Holder {
static final int ALTERNATIVE_HASHING_THRESHOLD;
static {
//擷取jdk内置的閥值
String altThreshold = java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction(
"jdk.map.althashing.threshold"));
int threshold;
try {
//設定目前閥值
threshold = (null != altThreshold)
? Integer.parseInt(altThreshold)
: ALTERNATIVE_HASHING_THRESHOLD_DEFAULT;
// disable alternative hashing if -1
if (threshold == -) {
threshold = Integer.MAX_VALUE;
}
if (threshold < ) {
throw new IllegalArgumentException("value must be positive integer.");
}
} catch(IllegalArgumentException failed) {
throw new Error("Illegal value for 'jdk.map.althashing.threshold'", failed);
}
ALTERNATIVE_HASHING_THRESHOLD = threshold;
}
}
//使用初始化容量和加載因子初始化HashMap
public HashMap(int initialCapacity, float loadFactor) {
if (initialCapacity < )
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
if (loadFactor <= || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
this.loadFactor = loadFactor;
threshold = initialCapacity;
init();
}
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
public HashMap() {
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
}
/*
* Constructs a new HashMap with the same mappings as the
* specified Map. The HashMap is created with
* default load factor (0.75) and an initial capacity sufficient to
* hold the mappings in the specified Map.
*/
public HashMap(Map<? extends K, ? extends V> m) {
this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + ,
DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
inflateTable(threshold);
putAllForCreate(m);
}
/**
* A randomizing value associated with this instance that is applied to
* hash code of keys to make hash collisions harder to find.
If 0 then alternative hashing is disabled.
*/
transient int hashSeed = ;
//工具函數,将number擴充成2的倍數
private static int roundUpToPowerOf2(int number) {
// assert number >= 0 : "number must be non-negative";
int rounded = number >= MAXIMUM_CAPACITY
? MAXIMUM_CAPACITY
: (rounded = Integer.highestOneBit(number)) !=
? (Integer.bitCount(number) > ) ? rounded << : rounded
: ;
return rounded;
}
//将表格大小擴充到toSize
private void inflateTable(int toSize) {
// Find a power of 2 >= toSize
int capacity = roundUpToPowerOf2(toSize);
//重新設定閥值
threshold = (int) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + );
//重新設定table
table = new Entry[capacity];
//根據capacity初始化hashSeed
initHashSeedAsNeeded(capacity);
}
// internal utilities
void init() {
}
/**
* Initialize the hashing mask value. We defer initialization until we
* really need it.
*/
final boolean initHashSeedAsNeeded(int capacity) {
boolean currentAltHashing = hashSeed != ;
//根據系統函數得到一個hash
boolean useAltHashing = sun.misc.VM.isBooted() &&
(capacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
boolean switching = currentAltHashing ^ useAltHashing;
//如果hashSeed初始化為0則跳過switching
//否則使用系統函數得到新的hashSeed
if (switching) {
hashSeed = useAltHashing
? sun.misc.Hashing.randomHashSeed(this)
: ;
}
return switching;
}
/*
雜湊演算法的核心:哈希函數
* Retrieve object hash code and applies a supplemental hash function to the
* result hash, which defends against poor quality hash functions. This is
* critical because HashMap uses power-of-two length hash tables, that
* otherwise encounter collisions for hashCodes that do not differ
* in lower bits. Note: Null keys always map to hash 0, thus index 0.
*/
*/
final int hash(Object k) {
int h = hashSeed;
//通過hashSeed初始化的值的不同來選擇不同的hash方式
if ( != h && k instanceof String) {
//String類采用不同的hash函數
return sun.misc.Hashing.stringHash32((String) k);
}
h ^= k.hashCode();
h ^= (h >>> ) ^ (h >>> );
return h ^ (h >>> ) ^ (h >>> );
}
//Returns index for hash code h.通過得到的hash值來确定它在table中的位置
static int indexFor(int h, int length) {
// assert Integer.bitCount(length) == 1 : "length must be a non-zero power of 2";
return h & (length-);
}
上面的hash()方法和indexFor()是hashMap當中的一個重點。
看到這麼多位操作,是不是覺得暈頭轉向了呢,還是搞清楚原理就行了,畢竟位操作速度是很快的,不能因為不好了解就不用了。
在哈希表容量(也就是buckets或slots大小)為length的情況下,為了使每個key都能在沖突最小的情況下映射到[0,length)(注意是左閉右開區間)的索引(index)内,一般有兩種做法:
- 方法1:讓length為素數,然後用hashCode(key) mod length的方法得到索引
- 方法2:讓length為2的指數倍,然後用hashCode(key) & (length-1)的方法得到索引
HashTable用的是方法1,HashMap用的是方法2。重點說說方法2的情況,方法2其實也比較好了解:
因為length為2的指數倍,是以length-1所對應的二進制位都為1,然後在與hashCode(key)做與運算,即可得到[0,length)内的索引。但是這裡有個問題,如果hashCode(key)的大于length的值,而且hashCode(key)的二進制位的低位變化不大,那麼沖突就會很多,舉個例子:
Java中對象的哈希值都32位整數,而HashMap預設大小為16,那麼有兩個對象那麼的哈希值分别為:0xABAB0000與0xBABA0000,它們的後幾位都是一樣,那麼與16異或後得到結果應該也是一樣的,也就是産生了沖突。造成沖突的原因關鍵在于16限制了隻能用低位來計算,高位直接舍棄了,是以我們需要額外的哈希函數而不隻是簡單的對象的hashCode方法了。具體來說,就是HashMap中hash函數幹的事了。
繼續分析源碼:
public int size() {
return size;
}
public boolean isEmpty() {
return size == ;
}
public V get(Object key) {
if (key == null)
return getForNullKey();
Entry<K,V> entry = getEntry(key);//檢視調用函數,在下面
return null == entry ? null : entry.getValue();
}
private V getForNullKey() {
if (size == ) {
return null;
}
for (Entry<K,V> e = table[]; e != null; e = e.next) {
if (e.key == null)
return e.value;
}
return null;
}
public boolean containsKey(Object key) {
return getEntry(key) != null;
}
final Entry<K,V> getEntry(Object key) {
if (size == ) {
return null;
}
//通過key的hash值确定table下标(null對應下标0)
int hash = (key == null) ? : hash(key);
//indexFor() = h & (length-1) = hash&(table.length-1)
for (Entry<K,V> e = table[indexFor(hash, table.length)];
e != null;
e = e.next)
//對沖突的處理辦法是将線性探查,即将元素放到沖突位置的下一個可用位置上
{
Object k;
/*注意:因為元素可能不是剛好存在它對應hash值得下一個位置
(如果該位置之前有元素,則要放在下兩個的位置,以此類推)
*/
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
//是以不僅要判斷hash還要判斷key(因為不同的key可能有相同的hash值)
return e;
}
return null;
}
/*
* 1. 通過key的hash值确定table下标
* 2. 查找table下标,如果key存在則更新對應的value
* 3. 如果key不存在則調用addEntry()方法
*/
public V put(K key, V value) {
if (table == EMPTY_TABLE) {
//初始化存儲表空間
inflateTable(threshold);
}
if (key == null)
return putForNullKey(value);
int hash = hash(key);
int i = indexFor(hash, table.length);
/*
注意:
我不斷的尋找,hash值對應位置之後的可用位置在哪裡
*/
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;
}
}
//上面的循環結束表示目前的key不存在與表中,需要另外增加
modCount++;
addEntry(hash, key, value, i);//函數在下面
return null;
}
/*
為減少篇幅,删除了一些功能實作類似的方法
大家可以自行閱讀分析
*/
/**
* Transfers all entries from current table to newTable.
*/
void transfer(Entry[] newTable, boolean rehash) {
int newCapacity = newTable.length;
for (Entry<K,V> e : table) {
while(null != e) {
Entry<K,V> next = e.next;
//是否重新進行hash計算
if (rehash) {
e.hash = null == e.key ? : hash(e.key);
}
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i];
newTable[i] = e;
e = next;
}
}
}
//擴充到指定的大小
void resize(int newCapacity) {
Entry[] oldTable = table;
int oldCapacity = oldTable.length;
if (oldCapacity == MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return;
}
Entry[] newTable = new Entry[newCapacity];
//重新hash
transfer(newTable, initHashSeedAsNeeded(newCapacity));
table = newTable;
threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + );
}
//Entry類就是一個簡單的鍵值對的類
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;//這是一種類似指針的東西
int hash;//還要存放hash值
/*
下面是一些十分基本的構造函數以及get,set方法
*/
Entry(int h, K k, V v, Entry<K,V> n) {
value = v;
next = n;
key = k;
hash = h;
}
public final K getKey() {
return key;
}
public final V getValue() {
return value;
}
public final V setValue(V newValue) {
V oldValue = value;
value = newValue;
return oldValue;
}
//必須要key和value都一樣才equals
public final boolean equals(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry e = (Map.Entry)o;
Object k1 = getKey();
Object k2 = e.getKey();
if (k1 == k2 || (k1 != null && k1.equals(k2))) {
Object v1 = getValue();
Object v2 = e.getValue();
if (v1 == v2 || (v1 != null && v1.equals(v2)))
return true;
}
return false;
}
public final int hashCode() {
return Objects.hashCode(getKey()) ^ Objects.hashCode(getValue());
}
public final String toString() {
return getKey() + "=" + getValue();
}
/**
* This method is invoked whenever the value in an entry is
* overwritten by an invocation of put(k,v) for a key k that's already
* in the HashMap.
*/
void recordAccess(HashMap<K,V> m) {
}
/**
* This method is invoked whenever the entry is
* removed from the table.
*/
void recordRemoval(HashMap<K,V> m) {
}
}
//根據需要,可能要擴容
//由于它由Put函數調用,調用之前已經确定表中沒有key的記錄
//addEntry預設目前表中沒有指定key的記錄,直接增加記錄
void addEntry(int hash, K key, V value, int bucketIndex) {
//計算存放位置
if ((size >= threshold) && (null != table[bucketIndex])) {
resize( * table.length);//将容量翻倍
hash = (null != key) ? hash(key) : ;
//尋找指定hash值對應的存放位置
bucketIndex = indexFor(hash, table.length);
}
createEntry(hash, key, value, bucketIndex);
}
//由于預設沒有key的記錄,是以直接增加
void createEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<>(hash, key, value, e);
size++;
}
//類似于Entry數組的疊代器,主要是對table進行操作
private abstract class HashIterator<E> implements Iterator<E> {
Entry<K,V> next; // next entry to return
int expectedModCount; // For fast-fail
int index; // current slot
Entry<K,V> current; // current entry
HashIterator() {
expectedModCount = modCount;
if (size > ) { // advance to first entry
Entry[] t = table;
while (index < t.length && (next = t[index++]) == null)
;
}
}
public final boolean hasNext() {
return next != null;
}
final Entry<K,V> nextEntry() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
Entry<K,V> e = next;
if (e == null)
throw new NoSuchElementException();
if ((next = e.next) == null) {
Entry[] t = table;
while (index < t.length && (next = t[index++]) == null)
;
}
current = e;
return e;
}
public void remove() {
if (current == null)
throw new IllegalStateException();
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
Object k = current.key;
current = null;
HashMap.this.removeEntryForKey(k);
expectedModCount = modCount;
}
}
private final class ValueIterator extends HashIterator<V> {
public V next() {
return nextEntry().value;
}
}
private final class KeyIterator extends HashIterator<K> {
public K next() {
return nextEntry().getKey();
}
}
private final class EntryIterator extends HashIterator<Map.Entry<K,V>> {
public Map.Entry<K,V> next() {
return nextEntry();
}
}
// Subclass overrides these to alter behavior of views' iterator() method
Iterator<K> newKeyIterator() {
return new KeyIterator();
}
Iterator<V> newValueIterator() {
return new ValueIterator();
}
Iterator<Map.Entry<K,V>> newEntryIterator() {
return new EntryIterator();
}
// Views
private transient Set<Map.Entry<K,V>> entrySet = null;
/**
* Returns a link Set view of the keys contained in this map.
*/
public Set<K> keySet() {
Set<K> ks = keySet;
return (ks != null ? ks : (keySet = new KeySet()));
}
private final class KeySet extends AbstractSet<K> {
public Iterator<K> iterator() {
return newKeyIterator();
}
public int size() {
return size;
}
public boolean contains(Object o) {
return containsKey(o);
}
public boolean remove(Object o) {
return HashMap.this.removeEntryForKey(o) != null;
}
public void clear() {
HashMap.this.clear();
}
}
/**
* Returns a Collection view of the values contained in this map.
*/
public Collection<V> values() {
Collection<V> vs = values;
return (vs != null ? vs : (values = new Values()));
}
private final class Values extends AbstractCollection<V> {
public Iterator<V> iterator() {
return newValueIterator();
}
public int size() {
return size;
}
public boolean contains(Object o) {
return containsValue(o);
}
public void clear() {
HashMap.this.clear();
}
}
/**
return a set view of the mappings contained in this map
*/
public Set<Map.Entry<K,V>> entrySet() {
return entrySet0();
}
private Set<Map.Entry<K,V>> entrySet0() {
Set<Map.Entry<K,V>> es = entrySet;
return es != null ? es : (entrySet = new EntrySet());
}
private final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
public Iterator<Map.Entry<K,V>> iterator() {
return newEntryIterator();
}
public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<K,V> e = (Map.Entry<K,V>) o;
Entry<K,V> candidate = getEntry(e.getKey());
return candidate != null && candidate.equals(e);
}
public boolean remove(Object o) {
return removeMapping(o) != null;
}
public int size() {
return size;
}
public void clear() {
HashMap.this.clear();
}
}
}
其他文章: JDK源碼學習(2)-TreeMap源碼分析
深入Java集合學習系列:HashMap的實作原理