An Introduction To HashMap In Java

Posted By : Sidharth Sagar | 22-Mar-2023

Java

Loading...

In Java, HashMap is a way to store the data in the form of key and value pair, where the key must be unique andif anyone inserts the duplicate key then it will replace the element of the corresponding key.
Updation and deletion types of operations are very easy to perform by using the key index. HashMap class exists in java.util package. HashMap implements the Map interface and inherits the AbstractMap class. It should allow us to store only one null key.


HashMap Constructors:
In Java, there are 4 types of HashMap constructors

1). HashMap<K, V> hm = new HashMap<K, V>();

2). HashMap<K, V> hm = new HashMap<K, V>(int initialCapacity);

3). HashMap<K, V> hm = new HashMap<K, V>(int initialCapacity, float loadFactor);

4). HashMap<K, V> hm = new HashMap<K, V>(Map map);


Advantages of HashMap:
1). Easy to use: In Java, it is very easy to implement HashMaps in our code.

2). Flexibility: HashMap is very flexible because it allows us to store key-value pairs of any data type.

3). Fast retrieval: It provides constant time access to elements, which means that the insertion and retrieval of elements are very fast.

4). Efficient storage: It uses a hashing function to map keys to indices in an array. So in this way, it allows efficient storage of data.

5). Suitable for large data sets: It is very suitable for a large type of data because it can handle this without slowing down.


Disadvantages of HashMap:
1). Not a thread-safe: if multiple threads access the same hashmap simultaneously, it can increase data inconsistencies. So, due to
For this reason, we can say hashmaps are not thread-safe.

2). Unordered: one of the disadvantages of using hashmap is that it is not ordered means the order in which we added the elements in it, is
not preserved.

3). Performance can degrade: if the load factor is high or the hashing function is not implemented properly then the performance can
degrade.

4). More complex than arrays or lists: For beginners, it is very difficult to understand and use this in comparison with lists or arrays.

5). Higher memory usage: In comparison with other data structures like lists or arrays, it uses more memory.

Operations on HashMap

1). Adding element:

import java.io.*;
import java.util.*;
class AddingElements {
public static void main(String args[])
{
HashMap<Integer, String> coursesMap = new HashMap<>();
coursesMap.put(1, "Java");
coursesMap.put(2, "Python");
coursesMap.put(3, "NodeJS");

System.out.println("Courses : "+ coursesMap);
}
}

Output :
Courses : {1=Java, 2=Python, 3=NodeJS}

2). Changing Elements:

import java.io.*;
import java.util.*;
class ChangingElements {
public static void main(String args[])
{
HashMap<Integer, String> coursesMap = new HashMap<Integer, String>();
coursesMap.put(1, "Python");
coursesMap.put(2, "Java");
coursesMap.put(3, "AWS");

System.out.println("Before Changes : " + coursesMap);

coursesMap.put(3, "NodeJS");

System.out.println("After Changes : " + coursesMap);
}
}

Output :
Before Changes : {1=Python, 2=Java, 3=AWS}
After Changes : {1=Python, 2=Java, 3=NodeJS}

3). Removing Element:

import java.io.*;
import java.util.*;
class RemovingElement {
public static void main(String args[])
{
HashMap<Integer, String> coursesMap = new HashMap<Integer, String>();
coursesMap.put(1, "Java");
coursesMap.put(2, "Python");
coursesMap.put(3, "NodeJS");
coursesMap.put(4, "Ruby");

System.out.println("Before Removing : "+ coursesMap);

coursesMap.remove(4);

System.out.println("After Removing : "+ coursesMap);
}
}

Output :
Before Removing : {1=Java, 2=Python, 3=NodeJS, 4=Ruby}
After Removing : {1=Java, 2=Python, 3=NodeJS}

4). Traversal of HashMap:

import java.util.HashMap;
import java.util.Map;

public class TraversalHashMap {
public static void main(String[] args)
{
HashMap<String, Integer> studentScoreMap = new HashMap<>();
studentScoreMap.put("Nitin", 25);
studentScoreMap.put("Ajit", 30);
studentScoreMap.put("Bobby", 20);

for (Map.Entry<String, Integer> st : studentScoreMap.entrySet()) {
System.out.println("Student's Name: " + st.getKey() + " Student's Score: " + st.getValue());
}
}
}

Output :
Student's Name: Nitin Student's Score: 25
Student's Name: Bobby Student's Score: 20
Student's Name: Ajit Student's Score: 30

At Oodles ERP, we provide custom enterprise software solutions to help enterprises steer clear of their routine operational challenges. Contact us at [email protected] to learn more about our custom ERP development services.