爲何Hashtable ConcurrentHashmap不支持key或者value爲null

  

  ConcurrentHashmap HashMapHashtable都是key-value存儲結構,但他們有一個不一樣點是 ConcurrentHashmapHashtable不支持key或者valuenull,而HashMap是支持的。爲何會有這個區別?在設計上的目的是什麼?數組

  ConcurrentHashmapHashtable都是支持併發的,這樣會有一個問題,當你經過get(k)獲取對應的value時,若是獲取到的是null時,你沒法判斷,它是putk,v)的時候valuenull,仍是這個key歷來沒有作過映射。HashMap是非併發的,能夠經過contains(key)來作這個判斷。而支持併發的Map在調用m.containskey)和m.get(key),m可能已經不一樣了。併發

HashMap.class:app

 

// 此處計算key的hash值時,會判斷是否爲null,若是是,則返回0,即key爲null的鍵值對
    // 的hash爲0。所以一個hashmap對象只會存儲一個key爲null的鍵值對,由於它們的hash值都相同。
    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }
    // 將鍵值對放入table中時,不會校驗value是否爲null。所以一個hashmap對象能夠存儲
    // 多個value爲null的鍵值對
    public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }
    final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        else {
            Node<K,V> e; K k;
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                e = p;
            else if (p instanceof TreeNode)
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            else {
                for (int binCount = 0; ; ++binCount) {
                    if ((e = p.next) == null) {
                        p.next = newNode(hash, key, value, null);
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            treeifyBin(tab, hash);
                        break;
                    }
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            if (e != null) { // existing mapping for key
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }

 

Hashtable.class:this

public synchronized V put(K key, V value) {
        // 確保value不爲空。這句代碼過濾掉了全部value爲null的鍵值對。所以Hashtable不能
        // 存儲value爲null的鍵值對
        if (value == null) {
            throw new NullPointerException();
        }
        // 確保key在table數組中還沒有存在。
        Entry<?,?> tab[] = table;
        int hash = key.hashCode(); //在此處計算key的hash值,若是此處key爲null,則直接拋出空指針異常。
        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;
}
相關文章
相關標籤/搜索