HashTable

56 阅读2分钟

HashTable

1.无参数构造函数,调用两个参数的构造函数,传递11与0.75

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

2.一个参数的构造函数调用两个参数的构造函数,传递指定量与0.75f

public Hashtable(int initialCapacity) {
        this(initialCapacity, 0.75f);
    }

3.两个参数的构造函数判断参数是否符题意,指定量为零,指定为一,创建指定量的数组,最大值与指定量乘以负载因子,得到最小值为负载量

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);
}

4.判断value是否为空,空错误(value不能为空),使用key的方法得到hash值(key不能为空的原因{空指针错误}),通过hash值计算存储位置,得到链头,开始循环判断添加,为空直接添加,不为空,则比较key(hash,与equals)相同替换,不相同继续

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)) {
            V old = entry.value;
            entry.value = value;
            return old;
        }
    }
​
    addEntry(hash, key, value, index);
    return null;
}

5.开始添加操作,判断添加元素有计数器,判断计数器与阈值的大小,大则扩容,小则,直接头插法添加

 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++;
    }
​

6.扩容创建新的容量为原本的2倍加1的数组,计算新的阈值,使用双层for循环将原本元素添加到新数组上,第一层循环数组,第二层循环链,数组,倒着走,链从头到尾,一个一个的挂上

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;
        }
    }
}

简言之:synchronized操作方法加锁,线程安全。

构造函数,创建默认为11,或者指定容量(指定容量为0则创建长度我1的数组)

添加过程,判断value为空报错,用key得:到hash值(key为空出现空指针异常),使用hash值计算位置,循环开始判断位置的元素,为空直接添加,不为空判断替换。添加前判断元素计数器,与阈值。超则库扩容。不超正常添加,头插法

扩容:直接创建2倍加1的新数组计算阈值0.75f*新容量双层for循环添加从后到前,链从上到下一个一个挂