Skip to content

Latest commit

 

History

History
104 lines (82 loc) · 3.16 KB

HashtableVsHashMap.md

File metadata and controls

104 lines (82 loc) · 3.16 KB

Hashtable 和 HashMap 的对比


Hashtable是线程安全的,每个方法上都有synchronized修饰.

Hashtable.put 不能为NULL.

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 = key.hashCode(); // key为NULL的话,这里抛出NPE
        // 找桶的位置也和HashMap不一样吧
        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;
    }

HashMap.put 可以为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;
    }

ConcurrentHashMap 的key,value也都不能为NULL.

🔥 所以呢,使用ConcurrentHashMap如果get返回NULL可以判定不存在;但是HashMap不能,因为可能你put进去的就是null.回忆下自己是不是犯过这种错误!

参考:

Differences between HashMap and Hashtable?