【手撕源码系列】HashMap源码解读—Java8版本
-
- 一、HashMap简介
- 二、定义
- 三、数据结构
- 四、域的解读
- 五、构造方法
- 六、核心方法
-
- 6.1 tableSizeFor(int cap)
- 6.2 hash() 方法
- 6.3 put(K key, V value)
- 6.4 resize()
- 6.5 putAll(Map<? extends K, ? extends V> m)
- 6.6 putIfAbsent(K key, V value)
- 6.7 remove(Object key)
- 6.8 remove(Object key, Object value)
- 6.9 "update()"
- 6.10 get(Object key)
- 6.11 containsKey(Object key)
- 6.12 containsValue(Object value)
- 6.13 getOrDefault(Object key, V defaultValue)
- 6.14 遍历
- 七、阿里面试实战
-
- 7.1、为什么需要散列表
- 7.2 能说一下HashMap的数据结构吗?
- 7.3 你对红黑树了解多少?为什么不用二叉树/平衡树呢?
- 7.4 红黑树怎么保持平衡的知道吗?
- 7.5 HashMap的put流程知道吗?
- 7.6 HashMap怎么查找元素的呢?
- 7.7 HashMap的哈希/扰动函数是怎么设计的?
- 7.8 为什么哈希/扰动函数能降hash碰撞?
- 7.9 为什么HashMap的容量是2的倍数呢?
- 7.10 如果初始化HashMap,传一个17的值new HashMap<>,它会怎么处理?
- 7.11 你还知道哪些哈希函数的构造方法呢?
- 7.12 解决哈希冲突有哪些方法呢?
- 7.13 为什么HashMap链表转红黑树的阈值为8呢?
- 7.14 扩容在什么时候呢?为什么扩容因子是0.75?
- 7.15 扩容机制了解吗?
- 7.16 jdk1.8对HashMap主要做了哪些优化呢?为什么?
一、HashMap简介
HashMap顶部有一段很长的注释,大概的介绍了HashMap。
1.1 原文
/**
* Hash table based implementation of the <tt>Map</tt> interface. This
* implementation provides all of the optional map operations, and permits
* <tt>null</tt> values and the <tt>null</tt> key. (The <tt>HashMap</tt>
* class is roughly equivalent to <tt>Hashtable</tt>, except that it is
* unsynchronized and permits nulls.) This class makes no guarantees as to
* the order of the map; in particular, it does not guarantee that the order
* will remain constant over time.