# HashMap in Java java 中 hashmap的实现原理。 * HashMap底层实现, hashmap的存储结构和操作? * hashmap的数组长度为什么要保证是2的幂? * hash冲突如何解决(链表和红黑树)为什么hashmap中的链表需要转成红黑树? * 扩容时机,什么时候会触发扩容?扩容时避免rehash的优化; * hashmap扩容时每个entry需要再计算一次hash吗? * hashmap扩容会引发什么问题,线上是否出现过类似的问题?如何避免扩容引发的问题? * jdk1.8之前并发操作hashmap时为什么会有死循环的问题? ## 数据结构 ``` public class HashMap extends AbstractMap implements Map, Cloneable, Serializable { static class Node implements Map.Entry { final int hash; final K key; V value; Node next; ... } static final class TreeNode extends LinkedHashMap.Entry { TreeNode parent; // red-black tree links TreeNode left; TreeNode right; TreeNode prev; // needed to unlink next upon deletion boolean red; } public V put(K key, V value) { return putVal(hash(key), key, value, false, true); } public V get(Object key) { Node e; return (e = getNode(hash(key), key)) == null ? null : e.value; } } ``` ## Hash冲突 ## 扩容 ## 问题