一、概述
问题导读:
- HashMap的特点、工作原理
- HashMap中get()和put()的原理,以及面试常问道的equals()和hashCode()的都有什么作用?
- hash的实现原理,为什么这样实现
- HashMap的大小超过了负载因子定义的容量,如何处理。
举个例子:
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("语文", 1);
map.put("数学", 2);
map.put("英语", 3);
map.put("历史", 4);
map.put("政治", 5);
map.put("地理", 6);
map.put("生物", 7);
map.put("化学", 8);
for(Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}具体内部结构(图片来源于:github.com/LRH1993/and…):
重要信息:
- 基于Map接口实现
- 允许key为null,也允许value为null,但是key只能存在一个;
- 非同步,即线程不安全
- 不保证有序(比如插入的顺序),也不保证序不随时间变化。
二、两个重要的参数
在HashMap中有两个很重要的参数,容量(Capacity,默认容量:16)和负载因子(Loadfactor,默认负载因子:0.75)
Capacity,即当前HashMap的目前的最大容量,而当目前已用的容量达到阈值(threshold = Capacity * Loadfactor)时,就要进行扩容,即执行resize操作,进行扩容是将容量乘以2。
三、put()方法的实现
put()方法大致的思路为:
- 对key的hashCode()做hash,再计算Index;
- 如果没碰撞直接放到bucket里;
- 若碰撞,则以链表的形式存储在buckets中
- 如果碰撞导致链表过长(大于等于TREEIFY_THRESHOLD),就把链表转换成红黑树;
- 若节点存在,就替换oldvalue(保证key的唯一性)
- 若bucke等于threshold,再进行插入时需先进行resize,再插入。
具体源码如下(来源于官网):
/**
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for the key, the old
* value is replaced.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return the previous value associated with <tt>key</tt>, or
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
* (A <tt>null</tt> return can also indicate that the map
* previously associated <tt>null</tt> with <tt>key</tt>.)
*/
public V put(K key, V value) {
//hash(key)计算key对应的hash值
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;
//判断table是否为空,为空使用resize()
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//直接插入到链表为空的结点出,否则则遍历链表进行插入
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
//计算index,并对null做处理
else {
Node<K,V> e; K k;
//key在table中链表的头结点处,则获得当前Node,否则进行从对应的链表中进行通过equal()方法进行查找
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//该链为树
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
//该链为链表
else {
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;
}
}
//判断当前key存在,存在则返回oldvalue
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
//否则判断,进行判断是否扩容,并插入
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
四、get()的实现
在理解put()方法后,get()方法就相对简单了。大致思路如下:
- bucket里的第一个节点,直接命中;
- 如果有冲突,则在树中通过key.equals(k)查找对应的entry
若为树,则在树中通过key.equals(k)查找,O(logn);
若为链表,则在链表中通过key.equals(k),O(n);
底层源码为:
/**
* Returns the value to which the specified key is mapped,
* or {@code null} if this map contains no mapping for the key.
*
* <p>More formally, if this map contains a mapping from a key
* {@code k} to a value {@code v} such that {@code (key==null ? k==null :
* key.equals(k))}, then this method returns {@code v}; otherwise
* it returns {@code null}. (There can be at most one such mapping.)
*
* <p>A return value of {@code null} does not <i>necessarily</i>
* indicate that the map contains no mapping for the key; it's also
* possible that the map explicitly maps the key to {@code null}.
* The {@link #containsKey containsKey} operation may be used to
* distinguish these two cases.
*
* @see #put(Object, Object)
*/
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
/**
* Implements Map.get and related methods
*
* @param hash hash for key
* @param key the key
* @return the node, or null if none
*/
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
//直接命中
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
//未命中,则从对应的hash值下的链表或树中进行查找
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;
}
五、hash方法的实现
在get()和put()的过程中,计算下标时,先对hashcode进行hash操作,然后再通过hash值进一步计算下标,如下图所示:
在对hashCode()计算hash时具体实现是这样的:
/**
* Computes key.hashCode() and spreads (XORs) higher bits of hash
* to lower. Because the table uses power-of-two masking, sets of
* hashes that vary only in bits above the current mask will
* always collide. (Among known examples are sets of Float keys
* holding consecutive whole numbers in small tables.) So we
* apply a transform that spreads the impact of higher bits
* downward. There is a tradeoff between speed, utility, and
* quality of bit-spreading. Because many common sets of hashes
* are already reasonably distributed (so don't benefit from
* spreading), and because we use trees to handle large sets of
* collisions in bins, we just XOR some shifted bits in the
* cheapest possible way to reduce systematic lossage, as well as
* to incorporate impact of the highest bits that would otherwise
* never be used in index calculations because of table bounds.
*/
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}从该方法中可以看出:高16bit不变,低16bit和高16bit做异或运算。
在设计hash方法时,因为目前的table长度为2的幂次方,而计算下标的时候,是这样实现的(使用 &位操作,而非%求余)(注意Hashtable集合使用的是%求余)
(n - 1) & hash这样做的原因是
- 使用(n - 1) & hash能降低hash冲突碰撞的概率,但为啥直接取模算法不是能保证吗!这是因为取模运行的效率低于hash运算;
- 因为hashcode视用来在散列存储结构中确定的存储地址,再根据key的hashcode值得到数组的下标查找,即用空间换时间。
- 高位和低位进行异或运算,能充分让大多数的hashcode的分布均匀。
获取HashMap的元素时,基本分两步:
- 首先根据hashcode()做hash运行,确定在table中的位置index;
- 如果table的节点的key不是待查找的元素,则通过keys.equals()在链表中查找。
在Java 8之前的实现中是用链表解决冲突,当产生碰撞的情况下,进行get()时,两步的时间复杂度为O(1) + O(n)(n为链表长度)。随着冲突的增加,O(n)的速度显然影响速度。
因而在Java中,利用红黑树替换链表,此时复杂度为O(1) + O(logn)。
六、resize()方法的实现
在put()方法中,如果发现当前的最大容量超过threshold时,则需要进行扩容,此时就使用resize()方法。
在扩容的过程中,hash值需要重新计算,具体如下:
因而元素在重新计算hash后,因为n变为2倍,那么n-1的mask范围在高位多1bit(红色),因而原来计算得到相同的hash值可能会不同,具体如下:
因此,在进行扩容的过程中,无需重新计算hash,只需要查看hash值新增的bit是1还是0,进行相应的链接,为0,保持不变;为1,则将该结点链接到心得结点处。
如下是16扩充为32的resize示意图:
该做法巧妙,且省去了重新计算hash()的过程,并且还能保住hash()的均匀分布。
具体源码如下:
/**
* 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) {
//容量为最大容量时,此时则直接将阈值设置为最大值,不重新计算hash值。
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);
}
if (newThr == 0) {
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
threshold = newThr;
//创建新的扩容Node数组
@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)
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
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;
//计算新的hash值保存不变,即最高位为0,保证在原索引处
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;
}
七、总结
- HashMap的特点
基于Map接口的实现,存储键值时,它可以接受null的key-value,非同步即线程不安全,HashMap存储着Entry(hash, key, value, next)对象
- HashMap工作原理
对于put()和get()方法,通过hash()方法得到计算index,并存储或读取对应的数值。
若存储时超过阈值threshold时,则需要扩容,则使用resize()进行扩容。
若发生碰撞冲突时,则使用链表或红黑树进行存储,链表变为红黑树的默认是8.
- get()和put()的原理,及equals()和hashcode()的作用
通过对key的hashcode()进行hash,并计算下标(n-1)&hash,得到在table中的位置index,若发生碰撞,则利用key.equals()方法进行比较在链表或树中查找对应的节点。
- hash的实现原理
在java 1.8版本中,通过hashcode()的高16位异或低16位实现,( h = k.hashcode()) ^ (h >>> 16),主要从效率、重复计算的速度等考虑,并能保证hash值高位和低位均匀分布。
注:本文中所有图片来源于:github.com/LRH1993/and…
若作者认为侵权,请通知我,立马删!!!