Java Hashmap: Points To Remember
Java Hashmap: Points To Remember
Java Hashmap: Points To Remember
Java HashMap class implements the Map interface which allows us to store key and
value pair, where keys should be unique. If you try to insert the duplicate key, it will
replace the element of the corresponding key. It is easy to perform operations using
the key index like updation, deletion, etc.
One object is used as a key (index) to another object (value). If you try to
insert the duplicate key, it will replace the element of the corresponding key.
Points to remember
o Java HashMap contains values based on the key.
o Java HashMap may have one null key and multiple null values.
o The initial default capacity of Java HashMap class is 16 with a load factor of
0.75.
HashMap(Map<? extends K,? It is used to initialize the hash map by using the
extends V> m) elements of the given Map object m.
HashMap(int capacity, float It is used to initialize both the capacity and load factor
loadFactor) of the hash map by using its arguments.
Methods of Java HashMap class
Method Description
void clear() It is used to remove all of the mappings from this map.
void putAll(Map map) It is used to insert the specified map in the map.
V putIfAbsent(K key, V It inserts the specified value with the specified key in the
value) map only if it is not already specified.
boolean This method returns true if some value equal to the value
containsValue(Object value) exists within the map, else return false.
boolean containsKey(Object This method returns true if some key equal to the key
key) exists within the map, else return false.
boolean equals(Object o) It is used to compare the specified Object with the Map.
V get(Object key) This method returns the object that contains the value
associated with the key.
boolean isEmpty() This method returns true if the map is empty; returns
false if it contains at least one key.
V replace(K key, V value) It replaces the specified value for a specified key.
int size() This method returns the number of entries in the map.
To get the key and value elements, we should call the getKey() and getValue() methods.
The Map.Entry interface contains the getKey() and getValue() methods. But, we should call
the entrySet() method of Map interface to get the instance of Map.Entry
Working of HashMap in Java
Hashing
It is the process of converting an object into an integer value. The integer value
helps in indexing and faster searches.
HashMap
HashMap contains an array of the nodes, and the node is represented as a class. It
uses an array and LinkedList data structure internally for storing Key and Value.
There are four fields in HashMap.
Example
In the following example, we want to insert three (Key, Value) pair in the HashMap.
HashMap<String, Integer> map = new HashMap<>();
map.put("Aman", 19);
map.put("Sunny", 29);
map.put("Ritesh", 39);
Let's see at which index the Key, value pair will be saved into HashMap. When we
call the put() method, then it calculates the hash code of the Key "Aman." Suppose
the hash code of "Aman" is 2657860. To store the Key in memory, we have to
calculate the index.
Calculating Index
Index minimizes the size of the array. The Formula for calculating the index is:
Index = hashcode(Key) & (n-1)
Where n is the size of the array. Hence the index value for "Aman" is:
1. Index = 2657860 & (16-1) = 4
The value 4 is the computed index value where the Key and value will store in
HashMap.
Hash Collision
This is the case when the calculated index value is the same for two or more Keys.
Let's calculate the hash code for another Key "Sunny." Suppose the hash code for
"Sunny" is 63281940. To store the Key in the memory, we have to calculate index
by using the index formula.
1. Index=63281940 & (16-1) = 4
The value 4 is the computed index value where the Key will be stored in HashMap.
In this case, equals() method check that both Keys are equal or not. If Keys are
same, replace the value with the current value. Otherwise, connect this node object
to the existing node object through the LinkedList. Hence both Keys will be stored at
index 4.
Similarly, we will store the Key "Ritesh." Suppose hash code for the Key is 2349873.
The index value will be 1. Hence this Key will be stored at index 1.
Suppose we have to fetch the Key "Aman." The following method will be called.
map.get(new Key("Aman"));
It generates the hash code as 2657860. Now calculate the index value of 2657860
by using index formula. The index value will be 4, as we have calculated above.
get() method search for the index value 4. It compares the first element Key with
the given Key. If both keys are equal, then it returns the value else check for the
next element in the node if it exists. In our scenario, it is found as the first element
of the node and return the value 19.
Buckets: Array of the node is called buckets. Each node has a data structure like a
LinkedList. More than one node can share the same bucket. It may be different in
capacity.