哈希表
哈希表(数组+链表/红黑树)
数组类型:java. util.HashMap$Node(表示一个单链表)
Class Node<K,V>{
K k;
V v;
Int Hash;
Node<K,V> next;
}
数组长度:16
先增元素元素
//hash值是根据元素的hashCode()方法得到了
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
//如果哈希表未初始化,就对其进行初始化
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//根据对象的哈希值计算对象的存储位置,如果该位置没有元素,就存储元素
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
//存入的元素和以前的元素先比较哈希值
如果哈希值不同,会继续向下执行把元素添加到集合
如果哈希值相同