Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how are buckets created in hashmap?

I am a bit confused about internal working of hash map. I have created a Hashmap with default capacity 16 and my key class always return a hash code value 1. So when i will enter the 13th element to this map it will double the map size.
1. How many buckets will be there in hash map ?
2. Does hash map create a new bucket on demand (i.e When hash code does not match to any existing bucket's hash code value)?

like image 350
Minion Avatar asked May 03 '15 19:05

Minion


People also ask

How many buckets are created in HashMap?

The Initial Capacity is essentially the number of buckets in the HashMap which by default is 24 = 16. A good HashMap algorithm will distribute an equal number of elements to all the buckets. Say we have 16 elements then each bucket will have 1 node, the search for any element will be achieved with 1 lookup.

How does HashMap determine bucket size?

The hashing function is applied to the key object to calculate the index of the bucket in order to store and retrieve any key-value pair. Capacity is the number of buckets in the HashMap. The initial capacity is the capacity at the time the Map is created. Finally, the default initial capacity of the HashMap is 16.

Why HashMap has 16 buckets?

Default capacity of Hashmap is 2^4 = 16 buckets. Let say we have well implemented hashcode() method, which make sure that key-value pair will be well distributed across 16 buckets equally. So, If there are 16 items in hashmap, then good hashcode method will distribute 1 item in each bucket.

How does HashMap calculate bucket index?

Calculating Index Formulae: Index = hashcode(Key) & (n-1) Where n is the size of the array. The key “Ankur” calculated index value is 11. This key and value pair store in one of the node of HashMap.


Video Answer


2 Answers

Although it's a bit late but this might help fellow users looking for an answer to this question.

As of your first question, How many buckets will be there in hash map ?-- There will be 16 buckets as that's the default capacity if you don't specify it in the constructor argument while instantiating HashMap.

Now let's come to the second question i.e. Does hash map create a new bucket on demand (i.e When hash code does not match to any existing bucket's hash code value)?

It will be interesting to look at the implementation of Hashmap's put method here.

public V put(K key, V value) {

    if (key == null)
        return putForNullKey(value);

    int hash = hash(key.hashCode());
    int i = indexFor(hash, table.length);

    for (Entry<K, V> e = table[i]; e != null; e = e.next) {
        Object k;

        if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
            V oldValue = e.value;
            e.value = value;
            e.recordAccess(this);
            return oldValue;
        }
    }
    modCount++;
    addEntry(hash, key, value, i);
    return null;
}

If you look closely, the method hash which accepts key's hashmap as argument is called inside the put method. The job of this method is to defend against poor quality hash functions . After this indexFor method is used, to identify the index (bucket) where the element will be inserted. The index returned by this method always points to an existing bucket. So, there isn't a case where the key's hashcode points to a bucket that doesn't exist.

So when does a hashmap creates a new bucket? It's neatly explained in javase docs.

It says: "An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. 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. "

Hope that helps.

like image 130
Ayushi Avatar answered Sep 18 '22 12:09

Ayushi


When you create a HashMap with the default capacity (16), you create it with 16 buckets (i.e., the capacity == the number of buckets).

  1. When the capacity is doubled, the number of buckets is doubled.

  2. The hashCode always matches some existing bucket, since modulus N (where N is the current capacity) is applied on the computed hash in order to find the bucket it belongs to.

like image 26
Eran Avatar answered Sep 21 '22 12:09

Eran