基于 JDK 1.8 分析 HashMap 的底层原理
Java 的 HashMap 可以说是用的最多、问的最多的一个 Map Collection 了。HashMap 是非同步的,即线程不安全。HashMap 允许存放的 key 为 null,但并不保证映射的顺序,也不保证这个顺序随时间保持不变。 本文对 HashMap 的代码分析基于 JDK 1.8 public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable 我们可以先从 HashMap 底层使用的数据结构了解 HashMap。 哈希表 + 链表 / 红黑树 static class Node<K,V> implements Map.Entry<K,V> { final int hash; final K key; V value; Node<K,V> next; // ... } /** * The table, initialized on first use, and resized as * necessary. When allocated, length is always a power of two....