源码解析之 HashMap与HashTable的区别

来源:互联网 发布:模拟退火算法伪代码 编辑:程序博客网 时间:2024/06/14 02:05

对于HashMap与HashTable的问题,可谓之面试之中被问到的频率较高的问题,该问题其实也是java基础知识的问题。通过源码的分析,加深我们对于他们之间的区别理解。

相似之处:

      1.两者实现的接口是相同的,但是继承的父类不同。

          可以看到HashTable 继承Dictionary<K,V>    

public class Hashtable<K,V>    extends Dictionary<K,V>    implements Map<K,V>, Cloneable, java.io.Serializable {
     而HashMap继承AbstractMap<K,V>

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

不同之处:

    在看不同之处时,不妨先看看两者的put(key k,value v )方法的源码

       2.1 HashMap的put(key k,value v)源码:

    public V put(K key, V value) {        if (table == EMPTY_TABLE) {            inflateTable(threshold);        }        if (key == null)            return putForNullKey(value);        int hash = hash(key);        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;    }    /**     * Offloaded version of put for null keys     */    private V putForNullKey(V value) {        for (Entry<K,V> e = table[0]; e != null; e = e.next) {            if (e.key == null) {                V oldValue = e.value;                e.value = value;                e.recordAccess(this);                return oldValue;            }        }        modCount++;        addEntry(0, null, value, 0);        return null;    }

我们再看看HashTable的put(key k,value v)方法的源码吧:

 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 = hash(key);        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;            hash = hash(key);            index = (hash & 0x7FFFFFFF) % tab.length;        }        // Creates the new entry.        Entry<K,V> e = tab[index];        tab[index] = new Entry<>(hash, key, value, e);        count++;        return null;    }

哈哈,这下就明显了吧。

1.hashtable 的put( k,v) 加上锁,也就意味着该方法是安全的,能够保证数据的安全性。hashMap对应的方法没有上锁,也就不安全。

2.对于value的值为null的处理。hashTable是不允许有null值,当有value=null的时候,throw new NullPointerException() 。对于hashMap出现value=null的时候,调用putForNullKey(V value)方法进行处理。

 private V putForNullKey(V value) {        for (Entry<K,V> e = table[0]; e != null; e = e.next) {            if (e.key == null) {                V oldValue = e.value;                e.value = value;                e.recordAccess(this);                return oldValue;            }        }

3.hashTable 是上版本留下的问题。一般不使用...











0 0
原创粉丝点击