Java源码分析:HashMap 1.8 相对于1.7 到底更新了什么?
1、1.8的实现由数组、链表、红黑树实现
默认初始容量 16,可以根据需要修改:
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
默认加载因子0.75,一般不改:
static final float DEFAULT_LOAD_FACTOR = 0.75f;
Node数组,数组中存的是Node节点:
transient Node<K,V>[] table;
Node类
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;
}
...
}
2、put方法
扩容,链表变红黑树[8]