环境jdk1.8.0_121html
与HashMap有几点区别(不了解HashMap的具体实现,看我另个博客http://www.cnblogs.com/dj3839/p/8111675.html)java
在HashMap中,冲突的值会在bucket造成链表,当达到8个,会造成红黑树,而在HashTable中,冲突的值就以链表的形式存储数组
public synchronized V put(K key, V value) { // Make sure the value is not null if (value == null) { throw new NullPointerException(); } // Makes sure the key is not already in the hashtable. Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; @SuppressWarnings("unchecked") Entry<K,V> entry = (Entry<K,V>)tab[index]; for(; entry != null ; entry = entry.next) { if ((entry.hash == hash) && entry.key.equals(key)) { V old = entry.value; entry.value = value; return old; } } addEntry(hash, key, value, index); return null; }
会发现求索引的方式也不同,(hash&0x7FFFFFFF)%tab.length,而在HashMap中是(hash^(hash>>>16))&(tab.length-1),能够看出HashTable里,并无作出相应的优化,这边解释下HashMap中的优化,(hash^(hash>>>16))这一步是实际上是让一个hash值的高16位和低16位作异或,混合高位和低位,加大低位的随机性,(hash^(hash>>>16))&(tab.length-1)求与,其实就是至关于HashTable中的取模,只是在你计算机中用位预算效率比较高,固然tab.length在HashMap中实际上是一个2的n次方,因此能达到这一的效果。安全
还有一点,能够看到HashTable中是不容许放值为Null的value,它会抛出错误。并且key值也不能为null,由于它直接拿key.hashCode(),null是拿不到hashCode也会发生错误。并发
继续看addEntry,开始添加元素函数
private void addEntry(int hash, K key, V value, int index) { modCount++; Entry<?,?> tab[] = table; if (count >= threshold) { // Rehash the table if the threshold is exceeded rehash(); tab = table; hash = key.hashCode(); index = (hash & 0x7FFFFFFF) % tab.length; } // Creates the new entry. @SuppressWarnings("unchecked") Entry<K,V> e = (Entry<K,V>) tab[index]; tab[index] = new Entry<>(hash, key, value, e); count++; }
代码很是简洁,若是数量大于限定值,就开始扩充,从新计算索引位置,而后插入高并发
先看插入优化
tab[index] = new Entry<>(hash, key, value, e);
在建立entry的时候,传了个bucket的第一个entry,this
protected Entry(int hash, K key, V value, Entry<K,V> next) { this.hash = hash; this.key = key; this.value = value; this.next = next; }
看构造函数其实能够看出,在这里进行指向旧的第一个entry,所以,在hashtable中实际上是插入在链表的头,而在HashMap是在尾spa
而后咱们在看它的rehash
protected void rehash() { int oldCapacity = table.length; Entry<?,?>[] oldMap = table; // overflow-conscious code int newCapacity = (oldCapacity << 1) + 1; if (newCapacity - MAX_ARRAY_SIZE > 0) { if (oldCapacity == MAX_ARRAY_SIZE) // Keep running with MAX_ARRAY_SIZE buckets return; newCapacity = MAX_ARRAY_SIZE; } Entry<?,?>[] newMap = new Entry<?,?>[newCapacity]; modCount++; threshold = (int)Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE + 1); table = newMap; for (int i = oldCapacity ; i-- > 0 ;) { for (Entry<K,V> old = (Entry<K,V>)oldMap[i] ; old != null ; ) { Entry<K,V> e = old; old = old.next; int index = (e.hash & 0x7FFFFFFF) % newCapacity; e.next = (Entry<K,V>)newMap[index]; newMap[index] = e; } } }
比HashMap简单的多。。。len扩充2*len+1,而后对原来bucket中的entry从新计算索引,并赋值,不改变链表原先的顺序,在HashMap中复杂的多,能够看我另个讲HashMap的博客
并且在hashtable中,调用构造函数时,直接初始化了里面的数组table,而在hashmap中是在进行put操做时,进行初始化,这个操做也在resize中
能够看下HashTable中的初始化方法
public Hashtable(int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); if (loadFactor <= 0 || Float.isNaN(loadFactor)) throw new IllegalArgumentException("Illegal Load: "+loadFactor); if (initialCapacity==0) initialCapacity = 1; this.loadFactor = loadFactor; table = new Entry<?,?>[initialCapacity]; threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1); }
还有一点,HashTable它的初始化,默认容量len是为11,后面也是2*len+1扩充,而HashMap是16,之后的扩充数量都是len*2,而且咱们提供容量大小时,也是会转成一个2的n次方,为何会有这样的区分,和它计算hash有关,在前面提到了(hash^(hash>>>16))&(tab.length-1),2^n-1,二进制就是n个1
public Hashtable() { this(11, 0.75f); }
但相对HashMap,HashTable是线程安全的,由于在不少方法,好比get,put,equals等,都使用了synchronized同步锁。
总结:
1. HashTable的key、value不能为null
2. HashTable线程安全
3. HashTable的优化其实没有HashMap作的好,在单线程的状况,最好使用HashMap
贴上一句源码中的提示
* Java Collections Framework</a>. Unlike the new collection * implementations, {@code Hashtable} is synchronized. If a * thread-safe implementation is not needed, it is recommended to use * {@link HashMap} in place of {@code Hashtable}. If a thread-safe * highly-concurrent implementation is desired, then it is recommended * to use {@link java.util.concurrent.ConcurrentHashMap} in place of * {@code Hashtable}.
大体意思就是:不须要线程安全用HashMap,须要线程安全的高并发用ConcurrentHashMap