reahashing of hash map in java collection framework

7
Apex T. G. India Pvt. Ltd Rehashing in HashMap Collection Framework

Upload: apex-tgi

Post on 28-Jun-2015

114 views

Category:

Technology


5 download

DESCRIPTION

Every HashMap has predefined size (Initial Capacity) and a logic to increment this size(Load Factor) whenever required(When threshold limit crossed).

TRANSCRIPT

Page 1: Reahashing of hash map in java collection framework

Apex T. G. India Pvt. Ltd

Rehashing in HashMap

Collection Framework

Page 2: Reahashing of hash map in java collection framework

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) .

Page 3: Reahashing of hash map in java collection framework

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.

Page 4: Reahashing of hash map in java collection framework

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).

Page 5: Reahashing of hash map in java collection framework

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. 

Page 6: Reahashing of hash map in java collection framework

1

Contact us

Page 7: Reahashing of hash map in java collection framework

Thanks

facebook.com/apex.tgi

twitter.com/ApextgiNoida

pinterest.com/apextgi

Stay Connected with us for more chapters on JAVA