当 HashMap 增加其大小时,HashMap 中值的索引会发生什么变化?

What happens to the index of the values in a HasMap when it increases its size?

我理解当我们像下面这样声明一个映射时:

Map <String, Integer> map = new HashMap ();

默认加载因子为0.75,大小为16,当map的buckets超过12个元素时,大小变为32。

但是,当使用 put 函数时,地图选择将放置对象的桶的索引的方式由 hascode % n 定义,但是当地图大小超过加载因子时会发生什么? n 不再具有相同的值,因此,如果在应用 hascode % n 时,结果索引将与之前不同,如何找到之前设置的条目?

我的最后一个问题是:

我们增加大小后桶的索引怎么会相同?

Default initial capacity of the HashMap takes is 16 and load factor is 0.75f (i.e 75% of current map size). The load factor represents at what level the HashMap capacity should be doubled.

例如容量和负载系数的乘积为16 * 0.75 = 12。这表示将第12个键值对存入HashMap后,其容量变为32.

For Further Exposure

进一步的过程说明

Rehashing of a hash map is done when the number of elements in the map reaches the maximum threshold value.

Usually the load factor value is 0.75 and the default initial capacity value is 16. Once the number of elements reaches or crosses 0.75 times the capacity, then rehashing of map takes place. In this case when the number of elements are 12, then rehashing occurs. (0.75 * 16 = 12)

When rehashing occurs a new hash function or even the same hash function could be used but the buckets at which the values are present could change. Basically when rehashing occurs the number of buckets are approximately doubled and hence the new index at which the value has to be put changes.

While rehashing, the linked list for each bucket gets reversed in order. This happens because HashMap doesn't append the new element at the tail instead it appends the new element at the head. So when rehashing occurs, it reads each element and inserts it in the new bucket at the head and then keeps on adding next elements from the old map at the head of the new map resulting in reversal of linked list.

If there are multiple threads handling the same hash map it could result in infinite loop.

Detailed explanation stating how infinite loop occurs in the above case can be found here :

Read this Article for more unserstanding

If the elements inserted in the map has to be sorted wrt the keys then TreeMap can be used. But HashMap would be more efficient if order of keys doesn't matter.

内部数据 结构被重建。来自文档:https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html :

When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.

Hashmaps do not preserve ordering. Look at using LinkedHashMaps 代替。

我想你问的是 "how can the index of the bucket be the same after we've increased the size?"

好吧,简单的答案是不能。 HashMap 必须在展开时对所有元素执行重新散列。

见以下方法:

/**
 * Transfers all entries from current table to newTable.
 */
void transfer(Entry[] newTable, boolean rehash) {

resize调用。

的 JavaDoc

Rehashes the contents of this map into a new array with a larger capacity. This method is called automatically when the number of keys in this map reaches its threshold.

强调我的

另请参阅:

  • Rehashing process in hashmap or hashtable
  • how and when Rehashing is done in HashMap