HashMap从懵懂到熟悉
什么是Hashmap
HashMap就是一个容器,用来存储数据,但是为什么不使用Arraylist,或者为什么不使用Link
数组的形式
link的形式
hashMap的形式
看到这里是不是有一种恍然大悟的感觉,hashMap其实就是数组加链表,我最开始的时候问过为什么不使用数组,
数组:使用数组你会发现他查询很快,但是增删改查效率非常低
链表:链表是非常快,但是对于增伤改查非常快,如果查询数组最好,并且链表非常消耗资源
Hashmap:而Hashmap出现就是结合了他们两个的优点※
提问问题
如果你把我下面的问题都会了,那说明你还不错哦,嘿嘿
- 1为什么使用hashcode?
- 2为什么会有数组,为什么用链表?
- 3什么时候扩容,如果进行扩容?
- 4什么时候使用使用红黑树?
- 5为什么hashcode要和节点进行与操作?
- 6数组长度多少,其他空间不可以占用,除非扩容?
- 7为什么判断hash和可以是否相等?
- 8整个体系的流程是什么?
代码
一般情况下我们都是这样使用HashMap
public static void main(String[] args){
HashMap<String,String> hashMap = new HashMap<>();
hashMap.put("judy","6688");
String judy = hashMap.get("judy");
System.out.println(judy);
}
源码分析
为什么使用HashCode
你有没有想过一个问题,如果我们使用hashMap,我们的下角标是如何生成的,难道我们使用的是随机数生成的下角标吗?那谁知道数据长度得有多大啊,
- 得到一个节点,重点看hash(key)
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
- 你会发现hashCode会与做与运行把低16位于高16位进行异或运算,为什么会这么做呢,这么做的时候我们会把前16给取出转换为二进制与h进行异或运算,这样就避免了出现重复的值,如果出现重复的值,可能会出现其中一个链表很长,但是其他位置根本没有数据,所以要把其他的位置都用起来.
下面的操作就是计算node节点到底在数组的什么位置?
n-1 & hash =====>hash%n , 得到的是0-15的区间, n的默认大小是16
0010101011001010010100101001
01111
最终的结果肯定是在16范围之内的.为什么用与是因为&与操作效率比较高
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
3通过第二步的操作大家有没有感到疑惑,如果是第2步操作得到的是15,那么这个时候二进制变为01110,这个时候不满足16,导致分布不匀均,那么如果才能分布均匀,所以数组的大家都是2的n次幂
在最开始的时候我们就给定默认的值,所以这里的16是有原因的,与操作的时候才会分布均匀
/**
* The default initial capacity - MUST be a power of two.
*/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
当看到这个的时候你想到了什么,我最开始的时候说要使用hashCode,而hashCode也就是从整个地方过来的,但是你有没有发现当拿到key的hashCode的谁还会进行16位的与操作,为什么在找数组下标(与操作)之前会使用hashCode在进行本身的异或?
原因就是为了避免出现重复的数据,为了node在数组中的位置分布均匀才做的这个操作
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
让他们两个之间进行异或,得到最后的hashcode
0101001010010高16位
0101001001001低16位
初始化数组
在这里只是声明了数组,但是并没有给数组赋值
/**
* The table, initialized on first use, and resized as
* necessary. When allocated, length is always a power of two.
* (We also tolerate length zero in some operations to allow
* bootstrapping mechanics that are currently not needed.)
*/
transient Node<K,V>[] table;
赋值语句
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//这个方法是判断是否需要初始化
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) {
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
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;
else { // zero initial threshold signifies using defaults
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
//这句话才是关键,所以默认赋给的值就是16,从最开始见解为什么长度必须是2N次幂到现在初值为16,大家应该理解了吧
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
//并且最后把值给了threshold
threshold = newThr;
把数据放到指定位置
如果是第一次存放
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
如果是key和hash相等,所以你会发现他到最后返回的以前的value
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//返回以前的value
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
如果使用红黑树
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
如果是在链表里
这个时候你会发现他其实就是循环链表,然后判断是否为空
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
如果这个时候链表的长度超出我们设置的链表长度8-1,那么这个时候就分配红黑树
static final int TREEIFY_THRESHOLD = 8;
什么时候扩容
看到这的时候大家有没有疑问,有没有想过数组长度只让用到75%,那么他怎么知道是不是需要扩容呢?
// 这块代码记录数组已经被占用了多少
//修改的次数
++modCount;
//size表示键值的数量//threshold表示容量
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
当他的size大于了threshold就会进行扩容,执行resize()操作,最开始我们进行数组初始化的时候也是执行的resize()操作
初始化或加倍表格大小。, 如果为null,则分配*符合字段阈值中保存的初始容量目标。
否则,因为我们使用的是2次幂扩展,所以每个bin中的*元素必须保持相同的索引,或者在新表中以2的偏移量移动*
他首先会判断你是否大于0 ,如果大于0,再判断是否大于最大值
if (oldCap > 0) {
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
//--------------------------扩容操作--------------
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold
}
//----------------------移动---当前地址加上原来容量的位置----------
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
/**
* The maximum capacity, used if a higher value is implicitly specified
* by either of the constructors with arguments.
* MUST be a power of two <= 1<<30.
*/
static final int MAXIMUM_CAPACITY = 1 << 30;
链表可以无限的伸展吗
你有没有想过这个问题? 假如hashmap的其中有一个位一直有node,那么这个时候会无限的向链表中添加数据,那么这个时候链表的数据就会变得很长,所以非常不好, 解决方案是设置规定的值
链表的长度不可以超过8
/**
* The bin count threshold for using a tree rather than list for a
* bin. Bins are converted to trees when adding an element to a
* bin with at least this many nodes. The value must be greater
* than 2 and should be at least 8 to mesh with assumptions in
* tree removal about conversion back to plain bins upon
* shrinkage.
*/
static final int TREEIFY_THRESHOLD = 8;
总结
好吧,下一步就是手写了…加油judy, 总归还是得感谢徐 X X ,❥(^_-)