一、Map体系
参考:http://java.chinaitlab.com/line/914247.html
Hashtable是JDK 5以前Map惟一线程安全的内置实现(Collections.synchronizedMap不算)。Hashtable继承的是 Dictionary(Hashtable是其惟一公开的子类),并不继承AbstractMap或者HashMap.尽管Hashtable和 HashMap的结构很是相似,可是他们之间并无多大联系。
ConcurrentHashMap是HashMap的线程安全版本,ConcurrentSkipListMap是TreeMap的线程安全版本。
最终可用的线程安全版本Map实现是ConcurrentHashMap/ConcurrentSkipListMap/Hashtable /Properties四个,可是Hashtable是过期的类库,所以若是能够的应该尽量的使用ConcurrentHashMap和 ConcurrentSkipListMap.html
二、Hashtable、HashMap异同java
(1)Hashtable是Dictionary的子类数组
代码以下:
public class Hashtable
extends Dictionary
implements Map, Cloneable, java.io.Serializable
HashMap:
public class HashMap
extends AbstractMap
implements Map, Cloneable, Serializable安全
HashMap和Hashtable都是Map接口的一个实现类;多线程
(2)Hashtable中的方法是同步的(),而HashMap中的方法在默认状况下不是同步的。便是说,在多线程应用程序中,不用专门的操做就安全地 可使用Hashtable了;而对于HashMap,则须要额外的同步机制。但HashMap的同步问题可经过Collections的一个静态方法得 到解决:app
public static Map synchronizedMap(Map m)ide
这个方法返回一个同步的Map,也就是说返回的Map是线程安全的。须要注意的是,对返回的map进行迭代时,必须手动在返回的map上进行同步,不然将会致使不肯定的行为:性能
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());
}this
(3)在HashMap中,null能够做为键,这样的键只有一个;能够有一个或多个键所对应的值为null。线程
当get()方法返回null值时,便可以表示HashMap中没有该键,也能够表示该键所对应的值为null。所以,在HashMap中不能由 get()方法来判断HashMap中是否存在某个键,而应该用containsKey()方法来判断。Hashtable的键值不能为null,否 则:java.lang.NullPointerException 。
(4)HashTable使用Enumeration,HashMap使用Iterator。
以上只是表面的不一样,它们的实现也有很大的不一样。
(5)HashTable中hash数组默认大小是11,增长的方式是 old*2+1。HashMap中hash数组的默认大小是16,并且必定是2的指数。
(6)哈希值的使用不一样,HashTable直接使用对象的hashCode,代码是这样的:
代码以下:
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
而HashMap从新计算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 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);
}
总之,HashTable是多线程安全的,不须要人工同步,但性能方面会差一点;而HashMap不能直接用于多线程。而
ConcurrentHashMap的出现正解决上诉问题。它是HashMap的线程安全版本,性能方面也优于HashTable。ConcurrentSkipListMap是TreeMap的线程安全版本。