1. Data Structure need to store the key-value pair.
Make an Entry class to store the HashMap Entries.
Variable: key, value and next
next (Entry) variable is used to avoid the collision of hashmap using chaining (linked list).
2. put() method to put new entries in hashmap.
Identify the bucket using hash (hashcode%SIZE)
- If no element exists in that bucket: put it as a new Entry.
- If element already exists: If element is duplicate, replace old one, otherwise, find the last element in the chain and add a new entry to the next pointer of the last element.
3. get() method : return the element in the hashmap
Identify the element bucket by calculating the hash (hashcode%SIZE) of the key, and return the element using the equals method.
4. Define size in power on 2
Rehashing is good when the size is exponential of 2.
Set initial size = 2^4 = 16.
To expanding HashMap, make size to it's double using power, i.e. 2^5= 32.
To reducing HashMap, make size to its half using power, i.e. 2^3= 8.