简介
北京的夏天不止是热,还让人浑身没劲,刚把上一期的Stroy做完,浑身酸的不想干活,前一段时间刚把ArrayList和LinkedList分析完毕,做了下记录,仔细想想还是接下来分析下HashMap的源码吧,这样后期有一个比较深刻的印象,来吧,老规矩,一个字:干。
HashMap 在我们日常写代码中,用到的还是蛮多的,时间久了,各种底层,各种算法都忘记差不多了,只记得是key-val 形式存储,其他都忘记差不多了。
概述
HashMap 最早出现在JDK1.2中,基于哈希表 Map 接口的实现,在JDK1.8之前,HashMap是采用数组+链表实现的,使用链表处理冲突,同一节点下的值都存储在链表里,这样有一个缺点,当元素较多的时候,就比较费劲了,JDK1.8及之后,优化了算法,由数组+链表+红黑树实现,当链表长度超过阈值8时,将链表转换为红黑树,这样大大提升了查找的时间。
数据结构

继承层次
public class HashMap<K,V> extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable {
HashMap 继承于AbstractMap,实现了Map、Cloneable、java.io.Serializable接口,Map接口定义了一组通用的操作;Cloneable接口则表示可以进行拷贝,在HashMap中,实现的是浅层次拷贝,即对拷贝对象的改变会影响被拷贝的对象;Serializable接口表示HashMap实现了序列化,即可以将HashMap对象保存至本地,之后可以恢复状态。
HashMap 底层是基于散列算法实现,散列算法分为散列再探测和拉链式(什么鬼😵),HashMap 使用了拉链式的散列算法。
是否线程安全
HashMap 的实现不是同步的,这意味着它不是线程安全的。key-value 形式进行存储,它的key、value都可以为null,但是只能是一个,并且key不允许重复(如果重复则新值覆盖旧值)。HashMap存入的顺序和遍历的顺序有可能是不一致的,保存数据的时候通过计算key的hash值来去决定存储的位置。
成员变量
// 默认的初始化数量是1<<4(16)
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
// 最大容量
static final int MAXIMUM_CAPACITY = 1 << 30;
// 默认的填充因子(loadFactor)大小
static final float DEFAULT_LOAD_FACTOR = 0.75f;
//当bucket(桶)节点上长度大于这个数值时候转换位红黑树
static final int TREEIFY_THRESHOLD = 8;
//当bucket(桶)节点上长度小于这个数值时,转换为链表
static final int UNTREEIFY_THRESHOLD = 6;
// 桶中结构转化为红黑树对应table最小
static final int MIN_TREEIFY_CAPACITY = 64;
//存储元素的数组,2的幂次倍
transient HashMap.Node<K, V>[] table;
// 存放具体元素
transient Set<Map.Entry<K, V>> entrySet;
// 存放元素的个数,注意这个不等于数组的长度
transient int size;
// 每次扩容和更改map结构的计数器 fail-fast机制
transient int modCount;
// 临界值,当实际大小size(容量*填充因子)超过临界时,会进行扩容
int threshold;
// 填充因子
final float loadFactor;
构造函数
HashMap(int, float)型构造函数
/**
* Constructs an empty <tt>HashMap</tt> with the specified initial
* capacity and load factor.
*
* @param initialCapacity the initial capacity
* @param loadFactor the load factor
* @throws IllegalArgumentException if the initial capacity is negative
* or the load factor is nonpositive
*/
public HashMap(int initialCapacity, float loadFactor) {
// 初始容量如果小于0,抛出异常提示
if (initialCapacity < 0) {
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
}
// 如果数组初始长度大于最大容量1<<30 ,也就是2^30,则设置最大长度为最大容量
if (initialCapacity > MAXIMUM_CAPACITY) {
initialCapacity = MAXIMUM_CAPACITY;
}
//如果填充因子小于或者等于0或者非数字,抛出异常
if (loadFactor <= 0 || Float.isNaN(loadFactor)) {
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
}
this.loadFactor = loadFactor;// 初始化填充因子
this.threshold = tableSizeFor(initialCapacity);//临界值大小
}
/**
* Returns a power of two size for the given target capacity.
* tableSizeFor(initialCapacity)返回大于initialCapacity的最小的二次幂数值
* <p>
* ```java先移位再或运算,最终保证返回值是2的整数幂```
* >>> 代表无符号右移,高位取0
*/
static final int tableSizeFor(int cap) {
int n = cap - 1;
n |= n >>> 1;
n |= n >>> 2;
n |= n >>> 4;
n |= n >>> 8;
n |= n >>> 16;
return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
}
HashMap(int)型构造函数
/**
* Constructs an empty <tt>HashMap</tt> with the specified initial
* capacity and the default load factor (0.75).
*
* @param initialCapacity the initial capacity.
* @throws IllegalArgumentException if the initial capacity is negative.
*/
public HashMap(int initialCapacity) {
// 调用HashMap(int,float)构造函数 装载因子的值采用默认的 0.75
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
HashMap()型构造函数
/**
* Constructs an empty <tt>HashMap</tt> with the default initial capacity
* (16) and the default load factor (0.75).
* 无参采用默认值
*/
public HashMap() {
// 初始化填充因子 0.75f
this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}
HashMap(Map<? extends K>)型构造函数
/**
* Constructs a new <tt>HashMap</tt> with the same mappings as the
* specified <tt>Map</tt>. The <tt>HashMap</tt> is created with
* default load factor (0.75) and an initial capacity sufficient to
* hold the mappings in the specified <tt>Map</tt>.
*
* @param m the map whose mappings are to be placed in this map
* @throws NullPointerException if the specified map is null
* 直接将map 放入HashMap中
*/
public HashMap(Map<? extends K, ? extends V> m) {
// 初始化填充因子
this.loadFactor = DEFAULT_LOAD_FACTOR;
// 将m中的所有元素添加至HashMap中
putMapEntries(m, false);
}
/**
* Implements Map.putAll and Map constructor
*
* @param m the map
* @param evict false when initially constructing this map, else
* true (relayed to method afterNodeInsertion).
* 将m所有元素放入HashMap中
*/
final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
// 存储m的大小
int s = m.size();
if (s > 0) {
// 如果table 为null,则代表未初始化
if (table == null) { // pre-size
// 根据要插入map的size,计算要创建HashMap的容量
float ft = ((float) s / loadFactor) + 1.0F;
int t = ((ft < (float) MAXIMUM_CAPACITY) ?
(int) ft : MAXIMUM_CAPACITY);
// 计算要创建的容量大小赋值给threshold
if (t > threshold) {
threshold = tableSizeFor(t);
}
} else if (s > threshold) {// 判断待插入m的size,如果大于阈值threshold,则进行resize 扩容处理
resize();
}
// 遍历m,将m中值添加到HashMap中
for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
K key = e.getKey();
V value = e.getValue();
putVal(hash(key), key, value, false, evict);
}
}
}
常用方法
put(K key, V value)
/**
* 指定Key,value,向HashMap存入数据
* @param key
* @param value
* @return
*/
public V put(K key, V value) {
//调用putVal()进行节点插入,hash值的计算,是调用hash(key)函数
return putVal(hash(key), key, value, false, true);
}
说明,HashMap并没有直接提供putVal接口给用户调用,而是提供put方法,put方法就是通过putVal来插入元素的。
putAll(Map<? extends K, ? extends V> m)
public void putAll(Map<? extends K, ? extends V> m) {
// 同构造函数HashMap(Map<? extends K, ? extends V> m) 一样,调用putMapEntries 方法,该方法已经在上述中说明
putMapEntries(m, true);
}
hash(Object key)
static final int hash(Object key) {
int h;
// 首先判断key是否为NULL,如果为NULL,返回0,如果不为NULL,获取key的hashCode()值,然后将hashCode值右移16位,将右移的值与原hashCode做异或运算
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
注:
key 的 hash值计算是通过hashCode()的高16位异或低16位实现的:(h = k.hashCode()) ^ (h >>> 16)
主要是从速度、功效、质量来考虑的,这么做可以在数组table的length比较小的时候
也能保证考虑到高低Bit都参与到Hash的计算中,同时不会有太大的开销。
putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict)

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K, V>[] tab;Node<K, V> p; int n, i;
//如果table(桶数组)未初始化或者长度为0,调用resize()进行扩容
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
// 通过(n - 1) & hash 计算,确定元素存放在哪个桶中,n一定是2的幂次,这个操作相当于hash % n 位运算更快
if ((p = tab[i = (n - 1) & hash]) == null)
//如果桶为空,创建新的键值对节点,存入table数组中
tab[i] = newNode(hash, key, value, null);
else {
//如果桶不为空,已经存在该元素,需要组成单链表或者红黑树
Node<K, V> e;
K k;
//插入元素与桶(bucket)中第一个元素对比,如果hash、key都相等,说明待插入元素和第一个元素相等,直接更新即可
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
//此时p指table[i]中存储的那个Node,如果待插入的节点中hash值和key值在p中已经存在,则将p值赋值给新节点e
e = p;
else if (p instanceof TreeNode)//当带插入节点与首元素中hash值和key值不相等,无该元素时,且TreeNode结构表示为红黑树结构,则按照红黑树结构进行插入
e = ((TreeNode<K, V>) p).putTreeVal(this, tab, hash, key, value);
else {//当带插入节点与首元素中hash值和key值不相等,无该元素时,且桶(bucket)是链表结构,则按照链表结构进行存储到尾部
// 在链表尾部插入节点
for (int binCount = 0; ; ++binCount) {
//遍历到链表尾部
if ((e = p.next) == null) {
//创建链表节点并插入
p.next = newNode(hash, key, value, null);
// 如果链表长度大于或者阈值8时,转化为红黑树
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
// 判断链表中的节点key值与插入的元素的key值是否相等
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
//遍历桶中链表,与前面的e = p.next组合,可以遍历链表将;即p调整为下一个节点
p = e;
}
}
//在桶(bucket)中找到key、hash值与插入元素key、hash相等时
if (e != null) { // existing mapping for key
// 记录e的value
V oldValue = e.value;
// 判断是否修改已插入节点的value
if (!onlyIfAbsent || oldValue == null)
// 新值替换旧值
e.value = value;
//访问后回调
afterNodeAccess(e);
// 返回旧值
return oldValue;
}
}
// 结构性修改
++modCount;
//键值对数量超过阈值时,则进行扩容
if (++size > threshold)
resize();
//插入后回调
afterNodeInsertion(evict);
return null;
}
注:hash 冲突发生的几种情况:
1.两节点 key 值相同(hash值一定相同),导致冲突;
2.两节点 key 值不同,由于 hash 函数的局限性导致hash 值相同,冲突;
3.两节点 key 值不同,hash 值不同,但 hash 值对数组长度取模后相同,冲突;
链表转换为红黑树
/**
* Replaces all linked nodes in bin at index for given hash unless
* table is too small, in which case resizes instead.
* 链表转换为红黑树
*/
final void treeifyBin(HashMap.Node<K,V>[] tab, int hash) {
int n, index; HashMap.Node<K,V> e;
// 桶(bucket)数组容量为空或者小于MIN_TREEIFY_CAPACITY 64,进行库容
if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
resize();
// 根据hash值计算索引值,遍历该索引位置的链表
else if ((e = tab[index = (n - 1) & hash]) != null) {
// hd 为头节点 t1为尾节点
HashMap.TreeNode<K,V> hd = null, tl = null;
do {
// 将普通节点替换为树形节点
HashMap.TreeNode<K,V> p = replacementTreeNode(e, null);
if (tl == null)//第一次循环获取头节点
hd = p;
else {
p.prev = tl; //当前节点的prev 属性设置为上一个节点
tl.next = p;// 上一个节点的next为当前节点
}
tl = p; // t1赋值为p,再下一次循环中作为上一个节点
} while ((e = e.next) != null);//e指向下一个节点
if ((tab[index] = hd) != null)
hd.treeify(tab);//头节点为根节点,构建红黑树
}
}
红黑树插入
/**
* Tree version of putVal.
*/
final TreeNode<K,V> putTreeVal(HashMap<K,V> map, Node<K,V>[] tab,
int h, K k, V v) {
Class<?> kc = null;
boolean searched = false;
TreeNode<K,V> root = (parent != null) ? root() : this;
// 从根节点开始查找合适的插入位置,索引位置的头节点不一定是红黑树的根节点(类似二分查找)
for (TreeNode<K,V> p = root;;) {// 根节点赋值给p,开始遍历
int dir, ph; K pk;
if ((ph = p.hash) > h)
dir = -1;//dir 小于0,接下来查找当前节点左孩子
else if (ph < h)
dir = 1;//大于0,查找当前节点的右孩子
else if ((pk = p.key) == k || (k != null && k.equals(pk)))// 传入的hash值key值与p节点相同 即p节点即为目标节点,返回p节点
return p;
else if ((kc == null && //要看该元素键是否实现了Comparable接口,再通过compareComparables方法来比较k 和pk的key 是否相等
(kc = comparableClassFor(k)) == null) ||
(dir = compareComparables(kc, k, pk)) == 0) {
if (!searched) {//在当前节点为根节点的树上搜索是否存在待插入的节点(该方法仅会执行一次)
TreeNode<K,V> q, ch;
searched = true;
if (((ch = p.left) != null &&// 从p节点左节点和右节点分别调用find进行查找,如果查找到目标节点则返回
(q = ch.find(h, k, kc)) != null) ||
((ch = p.right) != null &&
(q = ch.find(h, k, kc)) != null))
return q;
}
//否则使用定义的规则来比较k和pk节点key的大小,用来决定向左还是向右查找
dir = tieBreakOrder(k, pk);
}
TreeNode<K,V> xp = p;
if ((p = (dir <= 0) ? p.left : p.right) == null) {//找到了待插入节点的位置,xp为待插入节点的父节点
Node<K,V> xpn = xp.next;// 创建新的节点,其中x的next节点为xpn,即将x节点为xp的左孩子
TreeNode<K,V> x = map.newTreeNode(h, k, v, xpn);
if (dir <= 0) // 如果时dir <= 0, 则代表x节点为xp的左孩子
xp.left = x;
else
xp.right = x;//如果时dir> 0, 则代表x节点为xp的右孩子
xp.next = x;// 将xp的next节点设置为x
x.parent = x.prev = xp;// 将x的parent和prev节点设置为xp
if (xpn != null)
((TreeNode<K,V>)xpn).prev = x;
moveRootToFront(tab, balanceInsertion(root, x));// 红黑树的平衡调整
return null;
}
}
}
常用方法
public V get(Object key)
/**
* 根据key的hash值和key调用getNode
*/
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
Node<K,V> getNode(int hash, Object key)
/**
* 根据键值所在桶(bucket)的位置,然后再对链表或者红黑树进行查找
*/
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
// 定位key值对所在桶(bucket)的位置
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
// 桶(bucket)中第一项相等
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
// 桶中不止一个节点
if ((e = first.next) != null) {
// 类型为树节点,则在红黑树中进行查找
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
// 否则再链表中进行查找
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
TreeNode<K,V> getTreeNode(int h, Object k)
/**
*从根节点开始,调用find方法进行查找
*/
final TreeNode<K,V> getTreeNode(int h, Object k) {
return ((parent != null) ? root() : this).find(h, k, null);
}
TreeNode<K,V> find(int h, Object k, Class<?> kc)
/**
*红黑树进行查找
*/
final TreeNode<K,V> find(int h, Object k, Class<?> kc) {
TreeNode<K,V> p = this;
do {
int ph, dir; K pk;
TreeNode<K,V> pl = p.left, pr = p.right, q;
// hash值进行比较,若不同令当前节点变为左孩子或者右孩子
if ((ph = p.hash) > h)
p = pl;
else if (ph < h)
p = pr;
// hash值相同,则key值比较
else if ((pk = p.key) == k || (k != null && k.equals(pk)))
return p;
else if (pl == null)
p = pr;
else if (pr == null)
p = pl;
// 若k是可比较的并且k.compareTo(pk)返回结果不为0可进行下面else if
else if ((kc != null ||
(kc = comparableClassFor(k)) != null) &&
(dir = compareComparables(kc, k, pk)) != 0)
p = (dir < 0) ? pl : pr;
// 若k是不可比较的,并且k.compareTo(pk)返回结果为0,则在整棵树中进行查找
else if ((q = pr.find(h, k, kc)) != null)
return q;
else
p = pl;
} while (p != null);
return null;
}
V remove(Object key)
/**
* 根据key 调用removeNode方法进行删除
*/
public V remove(Object key) {
Node<K,V> e;
return (e = removeNode(hash(key), key, null, false, true)) == null ?
null : e.value;
}
Node<K,V> removeNode(int hash, Object key, Object value,
boolean matchValue, boolean movable)
/**
*移除节点
*/
final Node<K,V> removeNode(int hash, Object key, Object value,
boolean matchValue, boolean movable) {
Node<K,V>[] tab; Node<K,V> p; int n, index;
//定位key值对所在桶(bucket)的位置
if ((tab = table) != null && (n = tab.length) > 0 &&
(p = tab[index = (n - 1) & hash]) != null) {
Node<K,V> node = null, e; K k; V v;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
node = p;
// 待删除元素在桶(bucket)中,但不是桶中首元素
else if ((e = p.next) != null) {
if (p instanceof TreeNode)// 判断待删除元素是不是在红黑树结构的桶(bucket)中
node = ((TreeNode<K,V>)p).getTreeNode(hash, key);// 查找红黑树
else {//遍历链表,查找待删除的元素
do {
if (e.hash == hash &&
((k = e.key) == key ||
(key != null && key.equals(k)))) {
node = e;
break;
}//保存待删除节点的前一个节点,用于链表删除操作
p = e;
} while ((e = e.next) != null);
}
}
// matchValue为true,表示value必须相等才可进行删除
// matchValue为false,表示无需判断value,直接根据key进行删除
if (node != null && (!matchValue || (v = node.value) == value ||
(value != null && value.equals(v)))) {
if (node instanceof TreeNode)//红黑树结构删除
((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
else if (node == p)//待删除节点是桶链表头部,子节点放入
tab[index] = node.next;
else// 待删除节点在链表中间
p.next = node.next;
++modCount;
--size;
afterNodeRemoval(node);
return node;
}
}//待删除节点不存在,返回null
return null;
}
public boolean replace(K key, V oldValue, V newValue)
/**
* 根据key和旧的value查找匹配进行替换新value
*/
@Override
public boolean replace(K key, V oldValue, V newValue) {
Node<K,V> e; V v;
if ((e = getNode(hash(key), key)) != null &&
((v = e.value) == oldValue || (v != null && v.equals(oldValue)))) {
e.value = newValue;
afterNodeAccess(e);
return true;
}
return false;
}
一种是采用 putVal 的方法,因为很明显 putVal 是能够替换的,但是这里就涉及到了 size ,和 modCount 这两个 field 的变化了,也是要注意的。
public V replace(K key, V value)
/**
* 根据key查询匹配进行替换value
*/
@Override
public V replace(K key, V value) {
Node<K,V> e;
if ((e = getNode(hash(key), key)) != null) {
V oldValue = e.value;
e.value = value;
afterNodeAccess(e);
return oldValue;
}
return null;
}
另一种是 getNode 得到节点,然后替换,所以采用了 getNode 这个方法。
final Node<K,V>[] resize()
final Node<K,V>[] resize() {
// 保存当前table
Node<K,V>[] oldTab = table;
// 保存当前table的容量
int oldCap = (oldTab == null) ? 0 : oldTab.length;
// 保存当前的阈值
int oldThr = threshold;
// 新的table容量和阈值
int newCap, newThr = 0;
if (oldCap > 0) {//原table非空
if (oldCap >= MAXIMUM_CAPACITY) {// 当table容量超过最大值,则不再进行扩容
threshold = Integer.MAX_VALUE;
return oldTab;
}// 按照旧容量和阈值2倍计算新容量和新阈值大小
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
//阈值翻倍
newThr = oldThr << 1; // double threshold
}
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
// resize()函数在table为空被调用,oldCap小于等于0且oldThr等于0
//用户调用 HashMap()构造函数创建的HashMap,所有值均用默认值,oldTab(Table)表为空,oldCap为0,oldThr等于0
else { // zero initial threshold signifies using defaults
// 调用无参构造时,默认容量
newCap = DEFAULT_INITIAL_CAPACITY;
// 阈值为默认容量与默认负载因子乘积
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
if (newThr == 0) {//新阈值为0,阈值计算公式进行计算
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
threshold = newThr;
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];//创建新的桶数组
table = newTab;
if (oldTab != null) {//如果原桶数组不为空,遍历桶数组,将键值对映射到新的桶数组中
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
//若是单节点,直接在新数组中重新定位
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e;
else if (e instanceof TreeNode)//若节点为红黑树节点,要进行红黑树的rehash操作
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
// 若是链表,则进行链表的rehash操作
else { // preserve order
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
// 遍历链表,并将链表节点按原舒徐进行分组
do {
next = e.next;
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
else {
if (hiTail == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}
什么时候扩容:通过HashMap源码可以看到是在put操作时,即向容器中添加元素时,判断当前容器中元素的个数是否达到阈值(当前数组长度乘以加载因子的值)的时候,就要自动扩容了。
扩容(resize):其实就是重新计算容量;而这个扩容是计算出所需容器的大小之后重新定义一个新的容器,将原来容器中的元素放入其中。
经过rehash之后,元素的位置要么是在原位置,要么是在原位置再移动2次幂的位置
结束语
去年夏天的文章,后来给耽搁了,还有一部分没写完,后期再补充上。
网友评论