1. 程式人生 > >java中hashmap和hashtable的區別

java中hashmap和hashtable的區別

1、  繼承和實現區別

Hashtable是基於陳舊的Dictionary類的,HashMap是Java 1.2引進的Map介面的一個實現。

2、  執行緒安全不同

HashTable的方法是同步的,HashMap是未同步,所以在多執行緒場合要手動同步HashMap。

3、  對null的處理不同

HashTable不允許null值(key和value都不可以),HashMap允許null值(key和value都可以)。即HashTable 不允許null值其實在編譯期不會有任何的不一樣,會照樣執行,只是在執行期的時候Hashtable中設定的話回出現空指標異常。HashMap允許 null值是指可以有一個或多個鍵所對應的值為null。當get()方法返回null值時,即可以表示 HashMap中沒有該鍵,也可以表示該鍵所對應的值為null。因此,在HashMap中不能由get()方法來判斷HashMap中是否存在某個鍵,而應該用containsKey()方法來判斷。

4、  方法不同

HashTable有一個contains(Object value),功能和containsValue(Object value)功能一樣。
5、HashTable使用Enumeration,HashMap使用Iterator。

6、HashTable中hash陣列預設大小是11,增加的方式是 old*2+1。HashMap中hash陣列的預設大小是16,而且一定是2的指數。
7、雜湊值的使用不同,HashTable直接使用物件的hashCode,程式碼是這樣的:
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
而HashMap重新計算hash值,而且用與代替求模:
int hash = hash(k);
int i = indexFor(hash, table.length);
static int hash(Object x) {
  int h = x.hashCode();
  h += ~(h << 9);
  h ^= (h >>> 14);
  h += (h << 4);
  h ^= (h >>> 10);
  return h;
}
static int indexFor(int h, int length) {
  return h & (length-1);
}

區別

Hashtable

Hashmap

繼承、實現

Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable,Serializable

HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable,Serializable

執行緒同步

已經同步過的可以安全使用

未同步的,可以使用Colletcions進行同步Map Collections.synchronizedMap(Map m)

對null的處理

Hashtable table = new Hashtable();

table.put(null, "Null");

table.put("Null", null);

table.contains(null);

table.containsKey(null);

table.containsValue(null);

後面的5句話在編譯的時候不會有異常,可在執行的時候會報空指標異常具體原因可以檢視原始碼

public synchronized V put(K key, V value) {

     // Make sure the value is not null

  if (value == null) {

         throw new NullPointerException();

}

HashMap map = new HashMap();

map.put(null, "Null");

map.put("Null", null);

map.containsKey(null);

map.containsValue(null);

以上這5條語句無論在編譯期,還是在執行期都是沒有錯誤的.

在HashMap中,null可以作為鍵,這樣的鍵只有一個;可以有一個或多個鍵所對應的值為null。當get()方法返回null值時,即可以表示 HashMap中沒有該鍵,也可以表示該鍵所對應的值為null。因此,在HashMap中不能由get()方法來判斷HashMap中是否存在某個鍵,而應該用containsKey()方法來判斷。

增長率

   protected void rehash() {

      int oldCapacity = table.length;

      Entry[] oldMap = table;

      int newCapacity = oldCapacity * 2 + 1;

      Entry[] newMap = new Entry[newCapacity];

      modCount++;

      threshold = (int)(newCapacity * loadFactor);

      table = newMap;

      for (int i = oldCapacity ; i-- > 0 ;) {

          for (Entry<K,V> old = oldMap[i] ; old != null ; ) {

           Entry<K,V> e = old;

           old = old.next;

           int index = (e.hash & 0x7FFFFFFF) % newCapacity;

           e.next = newMap[index];

           newMap[index] = e;

          }

      }

    }

void addEntry(int hash, K key, V value, int bucketIndex) {

      Entry<K,V> e = table[bucketIndex];

        table[bucketIndex] = new Entry<K,V>(hash, key, value, e);

        if (size++ >= threshold)

            resize(2 * table.length);

    }

雜湊值的使用

HashTable直接使用物件的hashCode,程式碼是這樣的:

public synchronized boolean containsKey(Object key) {

      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)) {

           return true;

          }

      }

      return false;

    }

HashMap重新計算hash值,而且用與代替求模

      public boolean containsKey(Object key) {

              Object k = maskNull(key);

              int hash = hash(k.hashCode());

              int i = indexFor(hash, table.length);

              Entry e = table[i];

              while (e != null) {

                  if (e.hash == hash && eq(k, e.key))

                      return true;

                  e = e.next;

              }

              return false;

          }