2013-03-10 19:55:29.0|分类: java|浏览量: 1606
hashtable和hashtable的区别: (1)HashTable的方法是同步的,HashMap是未同步,所以在多线程场合要手动同步HashMap。 (2)HashTable不允许null值(value都不可以),HashMap允许null值(key和value都可以)。 (3)HashTable使用Enumeration,HashMap使用Iterator。 (4)HashTable中hash数组默认大小是11,增加的方式是 old*2+1。HashMap中hash数组的默认大小是16,而且一定是2的指数。 HashMap类中重要的代码:
package java.util; public class HashMap<K,V> extends AbstractMap<K,V>//(1)继承的类不一样 implements Map<K,V>, Cloneable, Serializable { static final int DEFAULT_INITIAL_CAPACITY = 16; public HashMap() { this.loadFactor = DEFAULT_LOAD_FACTOR; threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR); table = new Entry[DEFAULT_INITIAL_CAPACITY];//(2)默认的容量大小是16 init(); } public V put(K key, V value) {//(4)没有synchronized关键字,线程不安全 if (key == null)//(3)允许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; } public boolean containsKey(Object key) { return getEntry(key) != null; } public boolean containsValue(Object value) { if (value == null) return containsNullValue(); Entry[] tab = table; for (int i = 0; i < tab.length ; i++) for (Entry e = tab[i] ; e != null ; e = e.next) if (value.equals(e.value)) return true; return false; }Hashtable类中主要的部分:
package java.util; public class Hashtable<K,V> extends Dictionary<K,V>//(1)继承的类不一样 implements Map<K,V>, Cloneable, java.io.Serializable { public Hashtable() { this(11, 0.75f);//(2)默认的容器大小是11 } public synchronized V put(K key, V value) {//(4)put方法有关键字synchronized,线程安全 // Make sure the value is not null if (value == null) {//(3)值不能为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; for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { V old = e.value; e.value = value; return old; } } modCount++; if (count >= threshold) { // Rehash the table if the threshold is exceeded rehash(); tab = table; index = (hash & 0x7FFFFFFF) % tab.length; } // Creates the new entry. Entry<K,V> e = tab[index]; tab[index] = new Entry<K,V>(hash, key, value, e); count++; return null; } public synchronized boolean contains(Object value) { if (value == null) { throw new NullPointerException(); } Entry tab[] = table; for (int i = tab.length ; i-- > 0 ;) { for (Entry<K,V> e = tab[i] ; e != null ; e = e.next) { if (e.value.equals(value)) { return true; } } } return false; } } HashMap把Hashtable的contains方法去掉了,改成containsvalue和containsKey。因为Hashtable中contains方法容易让人引起误解。contains比较是value。 最大的不同是,Hashtable的方法是Synchronize的,而HashMap不是,在多个线程访问Hashtable时,不需要自己为它的方法实现同步,而HashMap 就必须为之提供外同步(Collections.synchronizedMap)。 因为Hashtable是线程安全的,并且使用了关键字synchronized,这就是的Hashtable的性能不如HashMap的高,所以假如对想成安全要求不是很高的话,建议使用HashMap。 |