hashmap 原理

总结一下java 的 HashMap

1.什么是HashMap及HashMap的特点?

键值对存储数据,最多允许一条数据键为null,值可以有多个null,非线程安全。

2.HashMap的原理

JDK1.8 中,HashMap是数组+链表+红黑树

首先看下部分源码

transient Node<K,V>[] table;

transient Set<Map.Entry<K,V>> entrySet;

transient int size;

transient int modCount;

int threshold; //所能容纳的数量极限

final float loadFactor;  //负载因子

static class Node<K,V> implements Map.Entry<K,V> {

final int hash;

    final K key;

    V value;

    Node<K,V> next;

    Node(int hash, K key, V value, Node<K,V> next) {

this.hash = hash;

        this.key = key;

        this.value = value;

        this.next = next;

    }

public final K getKey()        {return key; }

public final V getValue()      {return value; }

public final String toString() {return key +"=" +value; }

public final int hashCode() {

return Objects.hashCode(key) ^ Objects.hashCode(value);

    }

public final V setValue(V newValue) {

V oldValue =value;

        value = newValue;

        return oldValue;

    }

public final boolean equals(Object o) {

if (o ==this)

return true;

        if (oinstanceof Map.Entry) {

Map.Entry e = (Map.Entry)o;

            if (Objects.equals(key, e.getKey()) &&

Objects.equals(value, e.getValue()))

return true;

        }

return false;

    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容