总结一下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;
}
}