hjr-java源码:HashMap

理解

  • 散列算法:把多个数据用少量数据标记出来
  • hashing(哈希):标记是hashcode的散列算法

HashMap的结构是

包含多个HashCode的数组,每个HashCode对应一个Bucket(桶),每个桶是一个LinkList(链表),链表里面存储的是多个Entry(键值对+next+hash值)

数组中存取值 通过如下算法:

// 存储时:
int hash = key.hashCode(); 
int index = hash % Entry[].length;
Entry[index] = value;

// 取值时:
int hash = key.hashCode();
int index = hash % Entry[].length;
return Entry[index];

HashMpa.put(对象)

对象就是一个Entry,程序员控制键值对的内容,next和hash会自动生成

  • 碰撞:当系统给两个Entry生成的hash相同时就会发生碰撞,最后Entry,会存储到相同hash对应的链表里,其中 next,就是标记多个相同hash的Entry 之间的链表连接关系的。

next和hash是怎么自动生成的?

  • put()
    当程序员put一个Entry时,先使用Hash函数计算出下数组下标,确定这个Entry插入到hashcode数组的哪个位置
public V put(K key, V value) {
        if (key == null)
            return putForNullKey(value); //null总是放在数组的第一个链表中
        int hash = hash(key.hashCode());
        int i = indexFor(hash, table.length);
        //遍历链表
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            Object k;
            //如果key在链表中已存在,则替换为新value
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }
        modCount++;
        addEntry(hash, key, value, i);
        return null;
    }


void addEntry(int hash, K key, V value, int bucketIndex) {
    Entry<K,V> e = table[bucketIndex];
    table[bucketIndex] = new Entry<K,V>(hash, key, value, e); //参数e, 是Entry.next
    //如果size超过threshold,则扩充table大小。再散列
    if (size++ >= threshold)
            resize(2 * table.length);
}

  • get()
public V get(Object key) {
        if (key == null)
            return getForNullKey();
        int hash = hash(key.hashCode());
        //先定位到数组元素,再遍历该元素处的链表
        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.equals(k)))
                return e.value;
        }
        return null;
}

根据Key值做一次Hash映射,算出其对应的数组下标值,
碰撞问题,同一个位置的链表里可能存在多个Entry,这时就要从对应链表的头节点开始,一个个向下查找,直到找到对应的
Key值,这样就获得到了所要查找的键值对

  • hashcode数组的长度?
    有一个初始长度16,每当触发到75%,长度扩大一倍,然后把之前的数组全部数据移动到新数组中。

  • hashmap死锁
    在多线程的情况下,当重新调整HashMap大小的时候,就会存在条件竞争,因为如果两个线程都发现HashMap需要重新调整大小了,它们会同时试着调整大小

使用hashtable(完全同步)和currentHashMap(分片同步)等线程安全的可以避免死锁。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章