HashMap源码分析,为什么是无序的?

疑问:HashMap是无序的,怎么做到的?

先看一个现象

Map<String, Integer> m = new HashMap<>();
for(int i=0; i<10; i++) {
    m.put("key"+i, i);
}
System.out.println(m);

结果

{key1=1, key2=2, key0=0, key5=5, key6=6, key3=3, key4=4, key9=9, key7=7, key8=8}

这说明它的内部存储结构是无序的,那它存在哪里了呢?看下put方法的源码(JDK1.7),看倒数第二行

@Override public V put(K key, V value) {
    if (key == null) {
        return putValueForNullKey(value);
    }

    int hash = Collections.secondaryHash(key);
    HashMapEntry<K, V>[] tab = table;
    int index = hash & (tab.length - 1);
    for (HashMapEntry<K, V> e = tab[index]; e != null; e = e.next) {
        if (e.hash == hash && key.equals(e.key)) {
            preModify(e);
            V oldValue = e.value;
            e.value = value;
            return oldValue;
        }
    }

    // No entry for (non-null) key is present; create one
    modCount++;
    if (size++ > threshold) {
        tab = doubleCapacity();
        index = hash & (tab.length - 1);
    }
    addNewEntry(key, value, hash, index);
    return null;
}

key,value被传到addNewEntry方法

void addNewEntry(K key, V value, int hash, int index){
    table[index] = new HashMapEntry<K, V>(key, value, hash, table[index]);
}

它把key和value存到数组table中

transient HashMapEntry<K, V>[] table;

数组是有序的啊!在看下HashMapEntry的内部构造

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

奥,它有一个next,单向链表,这个next啥时候用的呢?在看下put方法,注意这三行的代码作用,求数据存在数组中的索引

int hash = Collections.secondaryHash(key);
HashMapEntry<K, V>[] tab = table;
int index = hash & (tab.length - 1);

hash和tab.length-1 与的结果就是的到一个tab长度范围内的一个值即求模。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值