}
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
//可以同时制定数组大小和负载因子
public HashMap(int initialCapacity, float loadFactor) {
…//省略部分逻辑判断
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
…
this.loadFactor = loadFactor;
threshold = initialCapacity;
…
}
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
int hash;
}
}
3.1.1 put
上面是HashMap的一些基本属性,都是相对比较重要的.接着我们来看一下添加元素put方法的实现,以下是JDK 1.7的put代码
public V put(K key, V value) {
//1. 数组为空 -> 初始化(创建)数组
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
//2. key为null,单独处理
if (key == null)
return putForNullKey(value);
//3. 计算hash值
int hash = hash(key);
//4. 计算该hash值该存放在数组的哪个索引处
int i = indexFor(hash, table.length);
//5. 遍历链表(数组的每个元素都是单链表的表头) 查找链表中是否已存在相同的key 如果有,则替换掉
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
//6. 添加元素到数组中
addEntry(hash, key, value, i);
return null;
}
3.1.1.1 inflateTable 数组初始化
简简单单几句代码涉及的东西缺特别多,我们逐个来解读一下.首先是初始化数组inflateTable方法,传入的是阈值.
private void inflateTable(int toSize) {
// Find a power of 2 >= toSize
int capacity = roundUpToPowerOf2(toSize);
threshold = (int) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);
table = new Entry[capacity];
initHashSeedAsNeeded(capacity);
}
private static int roundUpToPowerOf2(int number) {
// assert number >= 0 : “number must be non-negative”;
return number >= MAXIMUM_CAPACITY
-
? MAXIMUM_CAPACITY
- (number > 1) ? Integer.highestOneBit((number - 1) << 1) : 1;
}
//Integer.highestOneBit
public static int highestOneBit(int var0) {
//求掩码
var0 |= var0 >> 1;
var0 |= var0 >> 2;
var0 |= var0 >> 4;
var0 |= var0 >> 8;
var0 |= var0 >> 16;
//>>>:无符号右移。无论是正数还是负数,高位通通补0. 这里减了之后只剩下最高位为1
return var0 - (var0 >>> 1);
}
roundUpToPowerOf2方法是为了求一个比number大一点的2的幂次方的数,这里的代码看起来有点迷.它最后会求出数组应该初始化的长度,它可以自动将传入的容量转换为2的n次方.
Integer.highestOneBit是取传入的这个数的二进制形式最左边的最高一位且高位后面全部补零,最后返回int类型的结果.比如传入的是7(0111),则最后得到的是4(0100).它这里先将number-1,然后再左移一位,比如number是9,则number-1等于8(1000),左移一位等于10000就是16.这样最后它就将传入的容量转换为了2的n次方.
计算好了容量之后,计算阈值,然后初始化数组.
3.1.1.2 putForNullKey 添加null key
用了一个专门的方法用来
操作key为null的情况
/**
- Offloaded version of put for null keys
*/
private V putForNullKey(V value) {
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
if (e.key == null) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(0, null, value, 0);
return null;
}
将元素存放到了数组的第一个位置.第一个位置也是一个桶,这桶里面只有一个元素的key可以是null,其他元素都是被hash算法分配到这里来的.
3.1.1.3 计算hash值
/**
-
Retrieve object hash code and applies a supplemental hash function to the
-
result hash, which defends against poor quality hash functions. This is
-
critical because HashMap uses power-of-two length hash tables, that
-
otherwise encounter collisions for hashCodes that do not differ
-
in lower bits. Note: Null keys always map to hash 0, thus index 0.
*/
final int hash(Object k) {
int h = hashSeed;
if (0 != h && k instanceof String) {
return sun.misc.Hashing.stringHash32((String) k);
}
h ^= k.hashCode();
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
获取到了key的hashCode之后,又进行了一些骚操作,这里的hash算法设计得很神,这里的hash算法设计得好的话,则会大大减少hash冲突.
3.1.1.4 indexFor 计算元素在数组中的索引
/**
- Returns index for hash code h.
*/
static int indexFor(int h, int length) {
// assert Integer.bitCount(length) == 1 : “length must be a non-zero power of 2”;
return h & (length-1);
}
用hash值按位与数组长度-1,相当于 h % length.&运算比%效率高,所以这里是&运算来进行.为什么h & (length-1) = h % length
? 这其实与length有关,length上面说过,必须是2的幂.我们简单举个例子,h=2,length=8.
h & (length-1)
= 00000010 & 00000111
= 00000010
上面的最后结果是2 , 2 % 8
确实是等于2,验证完毕.
3.1.1.5 addEntry 添加元素到数组中
添加元素的时候可能之前这个位置是空桶,也可能之前这里的桶已经有元素存在了(hash冲突了).
/**
-
Adds a new entry with the specified key, value and hash code to
-
the specified bucket. It is the responsibility of this
-
method to resize the table if appropriate.
-
Subclass overrides this to alter the behavior of put method.
*/
void addEntry(int hash, K key, V value, int bucketIndex) {
//1. 键值对数量超过阈值 && 该索引处数组不为空(说明这里之前已经存在元素)
if ((size >= threshold) && (null != table[bucketIndex])) {
//扩容->原来的2倍
resize(2 * table.length);
hash = (null != key) ? hash(key) : 0;
bucketIndex = indexFor(hash, table.length);
}
//2. 创建Entry节点
createEntry(hash, key, value, bucketIndex);
}
//创建新的节点
void createEntry(int hash, K key, V value, int bucketIndex) {
//table[bucketIndex] 是放到新插入节点的后面,所以这里是头插法
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<>(hash, key, value, e);
size++;
}
键值对超过阈值就会扩容
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;
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);
}
//根据该节点hash值计算一下该节点该放到新数组的哪个索引处
int i = indexFor(e.hash, newCapacity);
//将桶内元素逐个转移到新的数组的新的索引处
//注意: 这里桶内顺序会倒过来.
//比如桶内是1->2->3 转移数据之后就是3->2->1
e.next = newTable[i];
newTable[i] = e;
e = next;
}
}
}
扩容之后就涉及到数据的迁移,迁移的时候需要重新计算节点在新数组中的位置,迁移完成还得更新一下阈值.
JDK 1.7中的put操作就是这些啦,东西还挺多的. 最核心的也就是put部分的代码,get的话比较简单这里就不做分析了.
3.2 JDK 1.8
基本上思路是差不多的,也是用数组+链表(or 红黑树)来装数据.
transient Node<K,V>[] table;
//链表长度超过8且数组长度大于64,则将链表转换成红黑树
static final int TREEIFY_THRESHOLD = 8;
//在1.8中节点改名字了… 改成了Node
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;
}
来看看它的put方法是怎么实现的
3.2.1 put
1.8的put方法稍微比1.7的看起来复杂些,但是不用怕,我们一句一句的分析
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
/**
-
Implements Map.put and related methods.
-
@param hash hash for key
-
@param key the key
-
@param value the value to put
-
@param onlyIfAbsent if true, don’t change existing value
-
@param evict if false, the table is in creation mode.
-
@return previous value, or null if none
*/
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
//1. table为空表时,创建数组 初始化. resize既是初始化也是扩容
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//2. 根据hash和数组长度求出元素应该在数组中的索引位置,如果此处为空则将节点放到这里
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
//3. 该索引处已经有节点存在且hash值和key都相等(需要替换value),则记录下该索引处的节点引用
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//4. 如果该索引处是红黑树,则将节点插入到树中
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
//5. 该索引处是链表
else {
//5.1 依次遍历链表
for (int binCount = 0; ; ++binCount) {
//5.2 找到链表尾部,将节点插入到尾部
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;
}
//5.3 找到key相等的了,则结束for循环,已在链表中找到需要替换value的节点
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
//6. 替换原来的值
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
//7. 超过阈值,则扩容
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
注释写得比较详细,这里与1.7的区别还是挺大的.
-
Java7中将节点插入链表是头插法,而Java8是尾插法
-
Java8中链表超过8且数组长度大于64则会将链表树化
-
Java7将key为null的单独处理,Java8没有单独处理(虽然它们的hash都是0,都是放数组第0处)
3.2.1.1 resize 扩容
首先来关注核心代码,扩容.
/**
-
Initializes or doubles table size. If null, allocates in
-
accord with initial capacity target held in field threshold.
-
Otherwise, because we are using power-of-two expansion, the
-
elements from each bin must either stay at same index, or move
-
with a power of two offset in the new table.
-
@return the table
*/
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
//老数组长度
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
//新数组长度 新阈值
int newCap, newThr = 0;
if (oldCap > 0) {
//老数组长度大于MAXIMUM_CAPACITY,则将阈值设置成Integer.MAX_VALUE 不扩容了…
//一般情况下,不会走到这个逻辑分支里面去
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
//1. 扩容: 将数组长度*2
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
//阈值也是*2
newThr = oldThr << 1; // double threshold
}
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else { // zero initial threshold signifies using defaults
//2. 初始化数组
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
if (newThr == 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) {
//3. 遍历旧数组
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
//3.1 该索引处 桶内只有一个元素,根据该节点的hash和新数组长度求出该节点在新数组中的位置,然后放置到新数组中
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e;
//3.2 该索引处为红黑树 单独处理
else if (e instanceof TreeNode)
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
//3.3 该索引处为单链表(链表长度小于8)
else { // preserve order
//不用挪动位置的链表,hash值&老数组长度为0,loHead为头部,loTail为尾部
Node<K,V> loHead = null, loTail = null;
//需要挪动位置的链表,hash值&老数组长度为1,hiHead为头部,hiTail为尾部
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
next = e.next;
//hash值&老数组长度
// 其实就是求最高位是0还是1,是0则保持原位置不动;是1则需要移动到 j + oldCap 处
//每条链表都被分散成2条,更分散
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;
}
//这些元素移动到了 老索引位置+oldCap 处
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}
resize的东西比较杂,即包含了初始化数组,也包含了扩容的逻辑.初始化数组比较简单,咱直接看一下扩容部分逻辑.首先是遍历原数组,此时原数组的每个索引处可能存在3种情况
-
该索引处桶内只有一个元素->根据该节点的hash和新数组长度求出该节点在新数组中的位置,然后放置到新数组中
-
该索引处为红黑树->单独处理
-
该索引处链表长度大于1,小于8
第3种情况比较复杂,这里单独分析一下.
分析前我们来看个东西,假设数组长度n=16,那么根据put部分的代码,存入数组时索引是(16 - 1) & hash
,这里有2个元素key1和key2,它们的hash值分别为5和21.下面是它们的计算过程
key1:
00001111 & 00000101 = 5
key2:
00001111 & 00010101 = 5
当数组扩容,n=32
key1:
00011111 & 00000101 = 00101 = 5
key2:
00011111 & 00010101 = 10101 = 5 + 16 = 21
扩容后n-1比以前多了1个1,这样会导致按位与的时候key2的位置变成了原位置+16的位置.因为我们使用的是2次幂的扩展,所以元素的位置要么在原位置,要么在原位置+2次幂的位置.
有了上面的分析之后,再回到我们3.3处的逻辑
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
} else {
if (hiTail == null)
最后
小编这些年深知大多数初中级Android工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人
都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
如果你需要这些资料, ⬅ 专栏获取
ey1和key2,它们的hash值分别为5和21.下面是它们的计算过程
key1:
00001111 & 00000101 = 5
key2:
00001111 & 00010101 = 5
当数组扩容,n=32
key1:
00011111 & 00000101 = 00101 = 5
key2:
00011111 & 00010101 = 10101 = 5 + 16 = 21
扩容后n-1比以前多了1个1,这样会导致按位与的时候key2的位置变成了原位置+16的位置.因为我们使用的是2次幂的扩展,所以元素的位置要么在原位置,要么在原位置+2次幂的位置.
有了上面的分析之后,再回到我们3.3处的逻辑
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
} else {
if (hiTail == null)
最后
小编这些年深知大多数初中级Android工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
[外链图片转存中…(img-HRRNXJTv-1719209786640)]
[外链图片转存中…(img-wNT0U8OL-1719209786640)]
[外链图片转存中…(img-0HIEH3SR-1719209786641)]
[外链图片转存中…(img-X7CgkxGc-1719209786642)]
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人
都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
如果你需要这些资料, ⬅ 专栏获取