java 集合源码探究之 hashtable 扩容、线程安全、遍历方式

类图

在这里插入图片描述

类信息

  • 不可以顺序访问

  • 底层使用数组来实现

    • Entry<?,?> 是一个链表
  • 也具有负载因子和阈值

  • 修改次数

  • 以及一些不序列化的属性

public class Hashtable<K,V>
    extends Dictionary<K,V>
    implements Map<K,V>, Cloneable, java.io.Serializable {
    private transient Entry<?,?>[] table;
    private transient int count;
    private int threshold;
    private float loadFactor;
    private transient int modCount = 0;
}

构造函数

  • 默认的初始化容量是 11 负载因子是 0. 75
  • 在构造函数中就完成了对hashtable的初始化
  • 默认的阈值 是 选取 initialCapacity * loadFactor 和 Integer.MAX_VALUE - 8 + 1的最小值
    public Hashtable() { 
    }

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

添加元素

  • value 不能为空
  • 计算出元素在数组中位置的方法
    • int index = (hash & 0x7FFFFFFF) % tab.length;
    public synchronized V put(K key, V value) {
        // Make sure the value is not null
        // value不能为空
        if (value == null) {
            throw new NullPointerException();
        }

        // Makes sure the key is not already in the hashtable.
        Entry<?,?> tab[] = table;
        int hash = key.hashCode();
        // 计算出 key再说数组中的位置
        int index = (hash & 0x7FFFFFFF) % tab.length;
        @SuppressWarnings("unchecked")
        // 得到这个hash位置的第一个元素
        Entry<K,V> entry = (Entry<K,V>)tab[index];
        // 开始遍历这个hash位置的所有元素
        for(; entry != null ; entry = entry.next) {
            // 如果出现 和 key 相等的元素,就直接覆盖掉, 返回.
            if ((entry.hash == hash) && entry.key.equals(key)) {
                V old = entry.value;
                entry.value = value;
                return old;
            }
        }
		// 到这里就意味着, 没有出现 key 相等的元素, 那么就直接在这个hash位置上添加即可
        addEntry(hash, key, value, index);
        return null;
    }

addEntry(int hash, K key, V value, int index)

  • count The total number of entries in the hash table.

  • 修改次数更新

  • 先判断是否需要扩容

    • 扩容条件: hash table 中 entries的数量大于等于 阈值
    • 初始化的时候计算阈值 threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
  • 非扩容的时候直接在当前hash位置添加即可

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

扩容

增加容量并对其进行内部重组哈希表,以便容纳和访问其条目更多效率高。当哈希表中的键数超出了该哈希表的容量以及荷载系数。

  • MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
  • 扩容一倍
    • 扩容的时候会重新计算 hash
 
    protected void rehash() {
        // 得到旧数组的长度
        int oldCapacity = table.length;
        Entry<?,?>[] oldMap = table;

        // overflow-conscious code
        // 新的容量为旧的2 倍+1
        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;
        }
        // 新建一个map 使用扩容后的大小
        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;
				// 重新计算  hash 确定元素在数组中的位置
                int index = (e.hash & 0x7FFFFFFF) % newCapacity;
                e.next = (Entry<K,V>)newMap[index];
                newMap[index] = e;
            }
        }
    }

得到元素

  • 计算 key的 hash值
  • 开始遍历这个hash位置的链表
    • key 的hash值相等, key的equlas相等
  • 找到的时候 返回一个空值
    public synchronized V get(Object key) {
        Entry<?,?> tab[] = table;
        // 计算 key的 hash值
        int hash = key.hashCode();
        int index = (hash & 0x7FFFFFFF) % tab.length;
        // 开始遍历这个hash位置的链表
        for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
            // 确定这个元素
            if ((e.hash == hash) && e.key.equals(key)) {
                return (V)e.value;
            }
        }
        return null;
    }

删除元素

  • 计算出hash值, 得到在数组中的位置
  • 遍历数组
    • 找到相等的key之后
    • 改变前后指针, 删除当前元素
    public synchronized V remove(Object key) {
        Entry<?,?> tab[] = table;
        // 计算出hash 值
        int hash = key.hashCode();
        int index = (hash & 0x7FFFFFFF) % tab.length;
 
        Entry<K,V> e = (Entry<K,V>)tab[index];
        for(Entry<K,V> prev = null ; e != null ; prev = e, e = e.next) {
            if ((e.hash == hash) && e.key.equals(key)) {
                modCount++;
                if (prev != null) {
                    prev.next = e.next;
                } else {
                    tab[index] = e.next;
                }
                count--;
                V oldValue = e.value;
                e.value = null;
                return oldValue;
            }
        }
        return null;
    }

遍历

遍历方式也都是使用的 Collections.synchronizedSet之类的集合来确保线程安全.

    public Set<K> keySet() {
        if (keySet == null)
            keySet = Collections.synchronizedSet(new KeySet(), this);
        return keySet;
    }
    public Collection<V> values() {
        if (values==null)
            values = Collections.synchronizedCollection(new ValueCollection(),
                                                        this);
        return values;
    }

    private class ValueCollection extends AbstractCollection<V> {
        public Iterator<V> iterator() {
            return getIterator(VALUES);
        }
        public int size() {
            return count;
        }
        public boolean contains(Object o) {
            return containsValue(o);
        }
        public void clear() {
            Hashtable.this.clear();
        }
    }
   
    public Set<Map.Entry<K,V>> entrySet() {
        if (entrySet==null)
            entrySet = Collections.synchronizedSet(new EntrySet(), this);
        return entrySet;
    }

小总结

  • 方法都被修饰为同步方法
  • 每次扩容 扩容 2 倍
  • 使用数组 + 链表的方式来存储元素
    • 拥有容量和负载
    • 数组中的entriy 大于等于 阈值的时候会扩容
    • 阈值 = 容量 * 负载因子
  • 判断 key相等的方法
    • 计算出 key的hashcode
    • 使用hashcode 和 表的长度相 % 计算出 key的hash值
    • 当hash值相等并且 key的equlas相等的时候 判断key相等
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值