J.J. Beam
J.J. Beam

Reputation: 3059

Will be HashMap capacity increased if I permanently storing objects in a single bucket?

HashMap has:

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.

So will it be capacity increased if I permanently storing objects in a single bucket ( make hashcode() always return 0)?

Upvotes: 0

Views: 54

Answers (2)

Sergey Afinogenov
Sergey Afinogenov

Reputation: 2202

As I understand, when amount of entries in HashMap (not buckets) becomes to be more than capacity * loadFactor, then amount of buckets will be increased twice, so HashMap will be rehashed and entries will be dustributed among new buckets.

Upvotes: 0

Thomas Kläger
Thomas Kläger

Reputation: 21435

Yes, it will still resize (see the source code of HashMap.putVal()):

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
               boolean evict) {
    // [code left out ...]

    ++modCount;
    if (++size > threshold)
        resize();
    afterNodeInsertion(evict);
    return null;
}

Upvotes: 1

Related Questions