Hashtable

Hashtable和HashMap基本上一样,通过查看源码,可以看出它们在设计上的区别

Hashtable的存储单元

    private static class Entry<K,V> implements Map.Entry<K,V> {
        final int hash;
        final K key;
        V value;
        Entry<K,V> next;

        protected Entry(int hash, K key, V value, Entry<K,V> next) {
            this.hash = hash;
            this.key =  key;
            this.value = value;
            this.next = next;
        }
    ... }

这里基本和HashMap一样,只是名字不一样

构造方法

    public Hashtable() {
        this(11, 0.75f);
    }

public Hashtable(int initialCapacity, float loadFactor) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal Load: "+loadFactor);

        if (initialCapacity==0)
            initialCapacity = 1;
        this.loadFactor = loadFactor;
        table = new Entry<?,?>[initialCapacity];
        threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
    }

hashTable的初始容量是11

 

再来看看Hashtable的put方法

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();
        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)) {//hash相同,键相同 修改值
                V old = entry.value;
                entry.value = value;
                return old;
            }
        }

        addEntry(hash, key, value, index);//向链表尾部添加键值对
        return null;
    }

private void addEntry(int hash, K key, V value, int index) {
        modCount++;

        Entry<?,?> tab[] = table;
        if (count >= threshold) {//达到阈值扩容
            // Rehash the table if the threshold is exceeded
            rehash();

            tab = table;
            hash = key.hashCode();
            index = (hash & 0x7FFFFFFF) % tab.length;
        }

        // Creates the new entry.
        @SuppressWarnings("unchecked")
        Entry<K,V> e = (Entry<K,V>) tab[index];
        tab[index] = new Entry<>(hash, key, value, e);
        count++;
    }

protected void rehash() {
        int oldCapacity = table.length;
        Entry<?,?>[] oldMap = table;

        // overflow-conscious code
        int newCapacity = (oldCapacity << 1) + 1;//容量提升一倍
        if (newCapacity - MAX_ARRAY_SIZE > 0) {
            if (oldCapacity == MAX_ARRAY_SIZE)
                // Keep running with MAX_ARRAY_SIZE buckets
                return;
            newCapacity = MAX_ARRAY_SIZE;
        }
        Entry<?,?>[] newMap = new Entry<?,?>[newCapacity];

        modCount++;
        threshold = (int)Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
        table = newMap;

        for (int i = oldCapacity ; i-- > 0 ;) {
            for (Entry<K,V> old = (Entry<K,V>)oldMap[i] ; old != null ; ) {
                Entry<K,V> e = old;
                old = old.next;

                int index = (e.hash & 0x7FFFFFFF) % newCapacity;
                e.next = (Entry<K,V>)newMap[index];
                newMap[index] = e;
            }
        }
    }

和HashMap不同的地方在于,

1.大部分Hashtable的方法是同步的

2.存储在数组的下标位置的算法不一样

int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;

而在hashMap中 如果key是null的话,会将hash值设为0,即hashMap可以存key为null的键值对

    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

HashMap下标值计算:i = (n - 1) & hash 是位运算,效率要高

3.扩容机制

Hashtable:int newCapacity = (oldCapacity << 1) + 1

新数组是原数组大小的 2n+1, 是奇数/素数,方便取模计算

HashMap:newCap = oldCap << 1

新数组是原数组的两倍,大小是2的n次幂,方便位运算

4.jdk1.8以后 hashMap做了红黑树优化

 

posted @ 2018-05-21 02:34  佳琪如梦  阅读(168)  评论(0编辑  收藏  举报