在Java8開始,當哈希衝突達到必定的程度,每個位置從鏈表轉化爲紅黑樹。性能
剛開始咱們在擴容的時候直接是2*M,它可能形成擴容後的哈希表分佈不均勻,能夠按着下面這個表格來設置M值。this
public class HashTable<K, V> { private final int[] capacity = {53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593, 49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469, 12582917, 25165843, 50331653, 100663319, 201326611, 402653189, 805306457, 1610612741 }; private static final int upperTol = 10; private static final int lowerTol = 2; private static final int initCapacity = 7; private int CapacityIndex = 0; private TreeMap<K, V>[] hashtable; private int size; private int M; //hash表的長度,即具體有多少個位置(選擇一個合適的素數) public HashTable(){ //this.M = M; this.M = capacity[CapacityIndex]; size = 0; hashtable = new TreeMap[M]; for(int i = 0 ; i < M ; i ++) hashtable[i] = new TreeMap<>(); } /*public HashTable(){ this(initCapacity); }*/ private int hash(K key){ //key.hashCode() & 0x7fffffff 取key.hashCode()的絕對值 return (key.hashCode() & 0x7fffffff) % M; } public int getSize(){ return size; } public void add(K key, V value){ TreeMap<K, V> map = hashtable[hash(key)]; if(map.containsKey(key)) //修改 map.put(key, value); else{ //添加 map.put(key, value); size ++; if(size >= upperTol * M && CapacityIndex+1 < capacity.length) //即size除以M >=upperTol //resize(2 * M); CapacityIndex ++; resize(capacity[CapacityIndex]); } } public V remove(K key){ V ret = null; TreeMap<K, V> map = hashtable[hash(key)]; if(map.containsKey(key)){ ret = map.remove(key); size --; if(size < lowerTol * M && CapacityIndex-1 >= 0) CapacityIndex --; //resize(M / 2); resize(capacity[CapacityIndex]); } return ret; } public void set(K key, V value){ TreeMap<K, V> map = hashtable[hash(key)]; if(!map.containsKey(key)) throw new IllegalArgumentException(key + " doesn't exist!"); map.put(key, value); } public boolean contains(K key){ return hashtable[hash(key)].containsKey(key); } public V get(K key){ return hashtable[hash(key)].get(key); } private void resize(int newM){ TreeMap<K, V>[] newHashTable = new TreeMap[newM]; for(int i = 0 ; i < newM ; i ++) newHashTable[i] = new TreeMap<>(); //因爲在hash()方法中有對M進行操做,在往新哈希表中存數據時應該用newM計算hash相應的hash值 int oldM = M; this.M = newM; for(int i = 0 ; i < oldM ; i ++){ TreeMap<K, V> map = hashtable[i]; for(K key: map.keySet()) newHashTable[hash(key)].put(key, map.get(key)); } this.hashtable = newHashTable; } }
哈希表的均攤複雜度爲O(1),有這麼好的性能其中一個緣由是它犧牲了順序性。spa