A HashMap with default capacity 16 can contain more than 11/16 objects without rehashing - Is this right?

两盒软妹~` 提交于 2019-12-13 08:06:36

问题


This is a followup question to What is the initial size of Array in HashMap Architecture?.

From that question I understand the initial capacity of HashMap is 16 by default which allows up to 11 entries before resizing since the default load factor is 0.75.

  1. When does the rehashing take place? After 11 entries in the array or 11 entries in one of the linked lists? I think after 11 entries in the array.

  2. Since the array size is 16 a HashMap could contain many objects (maybe more than 16) in a linked list as long as the array size is less than or equal to 11. Hence, a HashMap with default capacity 16 can contain more than 11/16 objects without rehashing - is this right?


回答1:


Hence, a HashMap with default capacity 16 can contain more than 11/16 objects(K,V) without rehashing

This is an obvious flaw in using the number of buckets occupied as a measure. Another problem is that you would need to maintain both a size and a number of buckets used.

Instead it's the size() which is used so the size() is the only thing which determines when rehashing occurs no matter how it is arranged.

From the source for Java 8

final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
    int s = m.size();
    if (s > 0) {
        if (table == null) { // pre-size
            float ft = ((float)s / loadFactor) + 1.0F;
            int t = ((ft < (float)MAXIMUM_CAPACITY) ?
                     (int)ft : MAXIMUM_CAPACITY);
            if (t > threshold)
                threshold = tableSizeFor(t);
        }
        else if (s > threshold)
            resize();



回答2:


I think you're fixating a bit too much on the implementation of HashMap, which can and does change over time. Think in terms of the map itself, rather than the internal data structures.

When does the rehashing take place? After 11 entries in the array or 11 entries in one of the linked lists? I think after 11 entries in the array.

Neither; the map is resized once the map contains 11 entries. Those entries could all be in their own buckets or all chained 11-deep in a single bucket.

Since the array size is 16 a HashMap could contain many objects (maybe more than 16) in a linked list as long as the array size is less than or equal to 11. Hence, a HashMap with default capacity 16 can contain more than 11/16 objects without rehashing - is this right?

No. While you could create your own hash table implementation that stores more elements than you have buckets, you'd do so at the cost of efficiency. The JDK's HashMap implementation will resize the backing array as soon as the number of elements in the map exceeds the load factor. It again doesn't matter whether the elements are all in the same bucket or distributed among them. in From the docs:

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.

For example if you have a HashMap (with default load and capacity) that currently contains 11 entries and you call .put() to insert a 12th entry, the map will be resized.



来源:https://stackoverflow.com/questions/40849888/a-hashmap-with-default-capacity-16-can-contain-more-than-11-16-objects-without-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!