目录
4、红黑树有什么特性?红黑树的时间复杂度是多少?理想中的HashMap的时间复杂度是多少?
6、谈一下hashMap中什么时候需要进行扩容,扩容resize()又是如何实现的?
结合问题:HashMap内部的结构是怎么样的?红黑树有什么特性?红黑树的时间复杂度是多少?理想中的HashMap的时间复杂度是多少?
相关文章:数据结构:HashMap
相关视频:https://www.bilibili.com/video/av75970633
1、HashMap内部的结构是怎么样的?体现在哪里?
数组的体现:
单项链表的体现:
红黑树的体现:
2、为什么HashMap的初始容量必须是2的倍数?
/**
* The default initial capacity - MUST be a power of two.
*/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
答案是为了方便实现"与"运算(&)
及时你传入的不是2的倍数,代码里也会自动帮你调整。
/**
* Returns a power of two size for the given target capacity.
*/
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;
}
3、Node里面有个hash的整型参数,为什么要保存?
键值对的意义自不用说,next是链表的标志,那么 int hash有什么用为什么要单独声明一个参数呢?
这是因为这个值的获取来之不易,经历了四个步骤才获取到,所以要保存一下:
- 保证HashMap的容量capacity是2的倍数,
- 获取存入值的HashCode
- 将HashCode的高16位和低16进行与运算,得到result
- 将上一步的result和(capacity-1)进行与运算。
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
3、什么情况下转为红黑树,代码体现?
代码体现:
static final int TREEIFY_THRESHOLD = 8;
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
...
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
...
}
4、红黑树有什么特性?红黑树的时间复杂度是多少?理想中的HashMap的时间复杂度是多少?
4.1、红黑树有什么特性?
4.2、红黑树的时间复杂度是多少?
4.3、理想中的HashMap的时间复杂度是多少?
O(logN)
其他面试题:
1. HashMap的底层原理是什么?线程安全么? 百度 美团
2. HashMap中put是如何实现的? 滴滴
4. 什么是哈希碰撞?怎么解决? 滴滴 美团
5. HashMap和HashTable的区别 小米
6. HashMap中什么时候需要进行扩容,扩容resize()是如何实现的? 滴滴
7. hashmap concurrenthashmap原理 美团
8. arraylist和hashmap的区别,为什么取数快?字节跳动
5、HashMap的底层原理是什么?线程安全么?
https://blog.csdn.net/songzi1228/article/details/99974540
HashMap底层是数组+链表,链表数据超过8个转为红黑树。不是线程安全的。作为对比,HashTable是线程安全的,因为HashTable对外提供的方法都加上了synchronized。
6、谈一下hashMap中什么时候需要进行扩容,扩容resize()又是如何实现的?
/**
* The load factor used when none specified in constructor.
*/
static final float DEFAULT_LOAD_FACTOR = 0.75f;
/**
* The next size value at which to resize (capacity * load factor).
*
* @serial
*/
int threshold;
// threshold 赋值
final Node<K,V>[] resize() {
...
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
threshold = newThr;
...
}
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
...
if (++size > threshold)
resize();
...
}
threshold [ˈθreʃhoʊld] 门槛。
有一个threshold的参数,一旦HashMap的size超过threshold就会触发扩容。
那么 resize的代码里又有什么逻辑呢?
参照咕泡学院公开课Jack老师 https://www.bilibili.com/video/av75970633。时间 1:04:10左右
/**
* 用于初始化或者将 size*2,即扩容
* 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) {
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
// newCap = oldCap << 1 容量在这里进行扩容,翻倍
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // 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);
}
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;
// 扩容后,把oldTab的数据复制到newTab中
// 专业术语叫做重新散列。大白话:把旧数组中的Node对象移动到新数组中
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
//a.只有一个元素
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e;
//c.它下面有红黑树
else if (e instanceof TreeNode)
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
//b.它下面有链表
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;
}
第一步,newCap = oldCap << 1 容量在这里进行扩容,翻倍;threshold也扩容,翻倍;
第二步, 扩容后,把oldTab的数据复制到newTab中。 专业术语叫做 重新散列。大白话:把旧数组中的Node对象移动到新数组中。
Node对象移动过程:
1.遍历数组下标(不为空的)
2.数组下标有元素,不为空
a.只有一个元素
b.它下面有链表
c.它下面有红黑树
7、什么是哈希碰撞?怎么解决?
哈希碰撞又叫做哈希冲突。
常用两种方法:链表法和开放寻址法
在哈希表中,每一个桶(bucket)或者槽(slot)都会对应一条链表,所有哈希值相同的元素放到相同槽位对应的链表中。
开放寻址法
核心思想:如果出现散列冲突,我们就重新探测一个空闲位置,再将元素插入。
一种比较简单的探测方法:线性探测法(Linear Probing)