reahashing of hash map in java collection framework
DESCRIPTION
Every HashMap has predefined size (Initial Capacity) and a logic to increment this size(Load Factor) whenever required(When threshold limit crossed).TRANSCRIPT
1
Rehashing in HashMap Every HashMap has predefined size (Initial Capacity) and a
logic to increment this size(Load Factor) whenever
required(When threshold limit crossed).
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) .
1
Rehashing in HashMap capacity is the number of buckets in the hash table.
initial capacity(Default Value is 16) is simply the capacity at
the time the hash table is created.
The load factor(Default value .75) is a measure of how full
the hash table is allowed to get before its capacity is
automatically increased.
1
Rehashing in HashMapFor Example :
Create HashMap with below configuration
Initial Capacity = 16 (Default Initial Capacity)
Load Factor : .75 (Default load factor)
Moment you add 13th element in given HashMap, Threshold
limit is crossed for given HashMap and system will create a
new backing keyset array(Size of this array will be double of
previous array).
1
Rehashing in HashMap System will have to again calculate exact bucket where
elements from previous bucket should be placed and all
elements from old HashMap will be copied to new HashMap.
This whole process is called ReHashing because Hashcode is
calculated for each element again.
Because overtime HashMap might be reHashed and order
could get change.
1
Contact us
Thanks
facebook.com/apex.tgi
twitter.com/ApextgiNoida
pinterest.com/apextgi
Stay Connected with us for more chapters on JAVA