1 初始容量 16
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4
2 容量最大值 1073741824
static final int MAXIMUM_CAPACITY = 1 << 30;
3 加载因子
static final float DEFAULT_LOAD_FACTOR = 0.75f;
4 当链表长度大于8的时候将链表转化为红黑树
static final int TREEIFY_THRESHOLD = 8;
5 当链表长度小于6的时候将红黑树转化为链表
static final int UNTREEIFY_THRESHOLD = 6;
6 当数组容量大于或者等于64的时候,并且链表长度大于4的时候,将链表转化为红黑树
static final int MIN_TREEIFY_CAPACITY = 64;
7 静态内部类,使用到了单向链表,保存了hash值(目的是为了下次扩容的时候,能够计算key在新的数组中的index值),键值对就存在这个静态内部类中
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 (o instanceof Map.Entry) { Map.Entry<?,?> e = (Map.Entry<?,?>)o; if (Objects.equals(key, e.getKey()) && Objects.equals(value, e.getValue())) return true; } return false; } }
8 table数组,类型为单向链表
transient Node<K,V>[] table;
9 HashMap的size
transient int size;
10 遍历集合的时候,防止多线程篡改数据
transient int modCount;
11 加载因子
final float loadFactor;