为了与1.8的HashMap进行比较,阅读并记录一下1.7的HashMap,但由于已经是过期的产物,就不看得太细
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 static final int MAXIMUM_CAPACITY = 1 << 30; static final float DEFAULT_LOAD_FACTOR = 0.75f; public HashMap(int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("Illegal initial capacity: " + initialCapacity); if (initialCapacity > MAXIMUM_CAPACITY) initialCapacity = MAXIMUM_CAPACITY; if (loadFactor <= 0 || Float.isNaN(loadFactor)) throw new IllegalArgumentException("Illegal load factor: " + loadFactor); this.loadFactor = loadFactor; threshold = initialCapacity; init(); } public HashMap(int initialCapacity) { this(initialCapacity, DEFAULT_LOAD_FACTOR); } public HashMap() { this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR); }
public V put(K key, V value) { // 1、table为空,则初始化 if (table == EMPTY_TABLE) { inflateTable(threshold); } // 2、可以将null作为key if (key == null) return putForNullKey(value); int hash = hash(key); // 3、根据hash找table对应存放的位置 int i = indexFor(hash, table.length); for (Entry<K,V> e = table[i]; e != null; e = e.next) { Object k; // 4、遍历查找,若找到,说明有重复,不需要插入,直接结束 if (e.hash == hash && ((k = e.key) == key || key.equals(k))) { V oldValue = e.value; e.value = value; e.recordAccess(this); return oldValue; } } modCount++; // 5、否则进行插入 addEntry(hash, key, value, i); return null; } void addEntry(int hash, K key, V value, int bucketIndex) { // 插入前先扩容 if ((size >= threshold) && (null != table[bucketIndex])) { resize(2 * table.length); // 又计算了一次hash值 hash = (null != key) ? hash(key) : 0; // 又获取了一次下标 bucketIndex = indexFor(hash, table.length); } // 插入元素 createEntry(hash, key, value, bucketIndex); } void createEntry(int hash, K key, V value, int bucketIndex) { // 先获取头节点 Entry<K,V> e = table[bucketIndex]; // 头插法插入新节点,上一步获取的头节点变成其next节点 table[bucketIndex] = new Entry<>(hash, key, value, e); size++; } private void inflateTable(int toSize) { // Find a power of 2 >= toSize // 将cap变成2的次方 int capacity = roundUpToPowerOf2(toSize); threshold = (int) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1); table = new Entry[capacity]; initHashSeedAsNeeded(capacity); }
resize根据newcap新建一个指定长度的table,transfer完成数组的复制
void resize(int newCapacity) { Entry[] oldTable = table; int oldCapacity = oldTable.length; if (oldCapacity == MAXIMUM_CAPACITY) { threshold = Integer.MAX_VALUE; return; } Entry[] newTable = new Entry[newCapacity]; transfer(newTable, initHashSeedAsNeeded(newCapacity)); table = newTable; threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1); }
void transfer(Entry[] newTable, boolean rehash) { int newCapacity = newTable.length; // 1、遍历table的每个位置的每个头节点 for (Entry<K,V> e : table) { while(null != e) { Entry<K,V> next = e.next; if (rehash) { e.hash = null == e.key ? 0 : hash(e.key); } int i = indexFor(e.hash, newCapacity); // 2、 非空则挨个头插法转移到新数组中,会导致顺序反转 e.next = newTable[i]; newTable[i] = e; e = next; } } }
不同 | 1.7 | 1.8 |
---|---|---|
插入元素 | 头插 | 尾插 |
扩容 | 先扩容再插入 | 先插入再扩容 |
结构 | 数组+链表 | 数组+链表+红黑树 |
复制 | 边hash边复制 | 先串连再复制 |
复制后节点顺序 | 相反 | 不变 |