Java Hashmap: Points To Remember

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Java HashMap

HashMap class is found in the java.util package.

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 contains only unique keys.

o Java HashMap may have one null key and multiple null values.

o Java HashMap is non synchronized.

o Java HashMap maintains no order.

o The initial default capacity of Java HashMap class is 16 with a load factor of
0.75.

Constructors of Java HashMap class


Constructor Description

HashMap() It is used to construct a default HashMap.

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) It is used to initializes the capacity of the hash map to


the given integer value, capacity.

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.

Set entrySet() It is used to return a collection view of the mappings


contained in this map.

Set keySet() It is used to return a set view of the keys contained in


this map.

V put(Object key, Object It is used to insert an entry in the map.


value)

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.

V remove(Object key) It is used to delete an entry for the specified key.

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.

Insert Key, Value pair in HashMap


We use put() method to insert the Key and Value pair in the HashMap. The default
size of HashMap is 16 (0 to 15).

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.

get() method in HashMap


get() method is used to get the value by its Key. It will not fetch the value if you
don't know the Key. When get(K Key) method is called, it calculates the hash code
of the Key.

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.

Let's fetch another Key "Sunny."


The hash code of the Key "Sunny" is 63281940. The calculated index value of
63281940 is 4, as we have calculated for put() method. Go to index 4 of the array
and compare the first element's Key with the given Key. It also compares Keys. In
our scenario, the given Key is the second element, and the next of the node is null.
It compares the second element Key with the specified Key and returns the value
29. It returns null if the next of the node is null.

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.

You might also like