HashMap详解

一、jdk1.7中HashMap的底层实现

       HashMap中数据是以数组+链表的形式存储的。它会把数据封装到内部类Entry类型的数组中。

transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;
static class Entry<K,V> implements Map.Entry<K,V> {
        final K key;
        V value;
        Entry<K,V> next;
        //key的hash值
        int hash;

        /**
         * Creates new entry.
         */
        Entry(int h, K k, V v, Entry<K,V> n) {
            value = v;
            next = n;
            key = k;
            hash = h;
        }

          如果当前放入的值是Data1,那Data1中的Entry<K,V> next就是Data2,Data2中的Entry<K,V> next就是Data3,具体代码实现可以看下面。

 public V put(K key, V value) {
        if (table == EMPTY_TABLE) {
            //初始化hashMap中的Entry数组
            inflateTable(threshold);
        }
        if (key == null)
            //如果key为null,就把值放在Entry[0]的位置
            return putForNullKey(value);
        //计算key的hash值           
        int hash = hash(key);
        //根据hash值和Entry数组的长度,计算数据放在Entry数组的哪个索引处        
        int i = indexFor(hash, table.length);
        
        //如果当前索引处有值了,就遍历链表
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            Object k;
            //如果链表中的Entry的key的hash值和当前key的hash值相同,且key的equals方法相等,就覆盖        
            //值,并且把旧值返回
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }

        modCount++;
        //将新数据添加到Entry数组中
        addEntry(hash, key, value, i);
        return null;
    }
    void addEntry(int hash, K key, V value, int bucketIndex) {
        
        if ((size >= threshold) && (null != table[bucketIndex])) {
            //如果当前所有的Entry的数量超过threshold,并且要新增的位置不为null,就把Entry数组扩    
            //容为原来的2倍大小
            resize(2 * table.length);
            //重新计算要插入的索引位置
            hash = (null != key) ? hash(key) : 0;
            bucketIndex = indexFor(hash, table.length);
        }
        //插入Entry数据
        createEntry(hash, key, value, bucketIndex);
    }
void createEntry(int hash, K key, V value, int bucketIndex) {
    //取出原来这个索引处的Entry
    Entry<K,V> e = table[bucketIndex];
    //将数据封装成新的Entry,放到链表的头部,并且把原来的Entry赋值给新的Entry的next属性
    table[bucketIndex] = new Entry<>(hash, key, value, e);
    //总的Entry的size加1
    size++;
}

  下面看一下扩容方法

void resize(int newCapacity) {
    Entry[] oldTable = table;
    int oldCapacity = oldTable.length;
    if (oldCapacity == MAXIMUM_CAPACITY) {
        threshold = Integer.MAX_VALUE;
        return;
    }
    //创建一个新大小的Entry数组
    Entry[] newTable = new Entry[newCapacity];
    //把老的数组中的值迁移到新的数组中
    transfer(newTable, initHashSeedAsNeeded(newCapacity));
    table = newTable;
    threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
}
void transfer(Entry[] newTable, boolean rehash) {
    int newCapacity = newTable.length;
    //遍历老的Entry数组
    for (Entry<K,V> e : table) {
        //遍历当前索引处的链表
        while(null != e) {
            //取得链表头的next对象
            Entry<K,V> next = e.next;
            if (rehash) {
                e.hash = null == e.key ? 0 : hash(e.key);
            }
            //计算链表头的Entry对象在新的数组中的位置,然后把该对象放在新位置的链表头部
            int i = indexFor(e.hash, newCapacity);
            e.next = newTable[i];
            newTable[i] = e;
            //循环链表剩余的部分
            e = next;
        }
    }
}

而get方法就很简单了,同样先根据key找到索引的位置,然后遍历链表,找到hash值相等并且equals相等的值。

    final Entry<K,V> getEntry(Object key) {
        if (size == 0) {
            return null;
        }
        
        int hash = (key == null) ? 0 : hash(key);
        for (Entry<K,V> e = table[indexFor(hash, table.length)];
             e != null;
             e = e.next) {
            Object k;
            if (e.hash == hash &&
                ((k = e.key) == key || (key != null && key.equals(k))))
                return e;
        }
        return null;
    }

二、jdk1.8中的底层实现待续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值