The HashMap in Java is a data structure that is effective for storing the key-value pairs. It effectively implements the Map interface in Java that maps a key to the specified value. The key in the Hashmap is taken as a unique entity. The HashMaps in Java are useful for large applications where there is a need to retrieve the data either from the databases or from the cache.
This article will elaborate on the details regarding the HashMap and its working.
How to Create HashMaps in Java?
The HashMaps in Java are responsible for representing a collection of the Map interface. The keys are the unique entities that store the values and can be retrieved when needed. The Java HashMaps are effective where the data can be retrieved using the key. The HashMaps are unsynchronized which means that the data is displayed without any sorting thus using less memory.
Below is the syntax of the Java HashMap:
HashMap<K, V> h = new HashMap<K, V>();
The syntax of the default constructor contains K as keys and V as values, as demonstrated in the above syntax. This article further explains the constructors and the operations of the Java HashMap.
Example 1: Implementation Using the Default Constructor of Java HashMap
The example below depicts the default implementation of HashMap in Java:
//Declare the required packages
import java.io.*;
import java.util.*;
class Example1 {
public static void main(String args[])
{
HashMap<Integer, String> h1 = new HashMap<>();
//pass the keys and the values to the keys for the hashmap
h1.put(1, "ID");
h1.put(2, "Designation");
h1.put(3, "Address");
//Print the result.
System.out.println("The values in h1 HashMap are : "
+ h1);
}
}
In the code above:
- The required Java packages are imported.
- The HahMap is initialized by using h1 as an object.
- The next step involves adding the key and the values for the respective keys using the put() method.
- The println() method prints the result of HashMap.
Output
The below output depicts that the Hashmap key and values are printed successfully:

Example 2: Implementation Using the HashMap(int initialCapacity) Constructor
This constructor takes an initial capacity of 6 and by default, the load factor is 0.75. It is important to note that if the threshold value increases the declared capacity is twice like in this case, it will increase up to 12. The code block below demonstrates the implementation of this constructor in Java.
//Declare the required packages
import java.io.*;
import java.util.*;
class hashmap {
public static void main(String args[])
{
//The initial capacity of the HashMap is declared.
HashMap<Integer, String> h1 = new HashMap<>(6);
HashMap<Integer, String> h2= new HashMap<Integer, String>();
//Declare the keys and the values for the hashmap
h1.put(1, "Car");
h1.put(2, "Bike");
h1.put(3, "Truck");
h1.put(4, "Cycle");
//Print the result.
System.out.println("The values in h1 HashMap are : "+ h1);
}
}
The above code is the same as the previous one except for a difference in that the initial capacity is declared for the HashMap.
Output
The below output shows the key and the value printed.

Example 3: Using the Initial Capacity and the LoadFactor
The code below shows the constructor of Java which contains an Initial capacity of 6 and a load factor of 0.5. By default the load factor is 0.75. Since we are providing different values as the load factor, therefore, the threshold value is 5*0.5 = 2.5. The value 2.5 is the threshold limit in our case.
//Declare the required packages
import java.io.*;
import java.util.*;
//Declare a class for the HashMap
class constructor {
public static void main(String args[])
{
//This initialization consists of the initial capacity and the load factor
HashMap<Integer, String> h1= new HashMap<>(6, 0.5f);
h1.put(1, "Python");
h1.put(2, "Sql");
h1.put(3, "Java");
//print the output using the println() method
System.out.println("The HashMap h1 has the mappings of: "
+ h1);
}
}
In the above code:
- The required packages for Java are imported.
- The class “constructor” is declared.
- The next step involves the initialization of HashMap using the initial capacity as 6 and the load factor as 0.75f. Here f stands for floating point.
- The put() method adds the elements to the HashMap.
- The println() method prints the key and the respective values.
Output
The below output consists of the keys and the values the key 1 corresponds to “Python”, 2 corresponds to Sql and 3 corresponds to Java.

Example 4: Using the Constructor (Map, Map)
This constructor creates the same mappings as that of the specified map.
//Declare the required packages
import java.io.*;
import java.util.*;
//Declare a class for the HashMap
class constructor {
public static void main(String args[])
{
//This initialization consists of (Map map) constructor
HashMap<Integer, String> h1= new HashMap<>();
h1.put(1, "ID");
h1.put(2, "Name");
h1.put(3, "Address");
//print the output using the println() method
System.out.println("The HashMap h1 has the mappings of: "
+ h1);
}
}
The code above is similar to the previous one except for a difference that the Map map constructor is used.
Output
The output below depicts the keys and the values of the HashMap printed.

Operations On HashMap in Java
There are certain operations that are performed on the HashMap as per the requirements. These operations are listed below along with their code examples to get a firm concept.
- Adding the element
- Removing the Key and the Value from the HashMap
- Change the entry of the HashMap
- Traversal in the HashMap
Operation 1: Adding Elements in Java HashMap
The below code shows the implementation of the put() method to add elements to the Java HashMap.
import java.io.*;
import java.util.*;
class Addexample {
public static void main(String args[])
{
HashMap<Integer, String> h = new HashMap<>();
// Use put() method to insert the key-value pair
h.put(1, "Hello");
h.put(2, "to");
h.put(3, "Coding");
h.put(4, "in");
h.put(5, "Java");
//Print the output of the javaHashMap
System.out.println("The Key to Value Mappings are : "
+ h);
}
}
In the above block of code:
- The required packages are imported.
- In the next step, the class is declared to add the elements.
- Using the put() method, the elements are added to the HashMap.
- The output is printed accordingly.
Operation 2: Remove the Key and Value From the HashMap
The below code implements the remove() method to discard the value along with its key.
import java.io.*;
import java.util.*;
class Removeexample {
public static void main(String args[])
{
HashMap<Integer, String> h = new HashMap<>();
// Add Elements using put method
h.put(1, "Hello");
h.put(2, "to");
h.put(3, "Coding");
h.put(4, "in");
h.put(5, "Java");
System.out.println("The Key to Value Mappings are : "
+ h);
h.remove(2);
//Print the output of the javaHashMap
System.out.println("The HashMap after removal is"
+ h);
}
}
In the above code:
- The required packages are imported and the class to remove an entry from HahMap is declared.
- The entries are made using the put() method in Java.
- The remove() method in the above code removed an element “to” from the HashMap.
- The print method prints the HashMap before removing the element and after removing the element.
Output
The below output declares the HashMap with different entries whereas the Hahmap is printed as the output and the Hashmap after the entry is deleted is printed.

Operation 3: Change the Entry of the HashMap
The below code describes the method to change the element from the Java HashMap. The key is declared and the new value to be changed is mentioned.
import java.io.*;
import java.util.*;
class changeexample {
public static void main(String args[])
{
HashMap<Integer, String> h = new HashMap<>();
// The elements are inserted using the put() method. h.put(1, "Python");
h.put(2, "Java");
h.put(3, "Database");
h.put(4, "C++");
h.put(5, "HTML");
System.out.println("The Key to Value Mappings are : "
+ h);
h.put(5, "PHP");
//Print the output of the javaHashMap
System.out.println("The HashMap after changing the value is "
+ h);
}
}
In the above code, the entries are added using the put() method. The put() method, when declared with a key and new value, changes the previous element with the new element at that specific index.
Output
The below output shows that the key at index 5 contains the value of “HTML”, when the value is changed to “Php”, the result is printed as “PHP” with key 5.

Operation 4: Traversing through the HashMap
We can iterate over a Hahmap of Java to traverse it. The code below prints the entries along with the key and the values respectively.
import java.util.HashMap;
import java.util.Map;
public class Traversalinhashmap {
public static void main(String[] args)
{
HashMap<Integer, String> m = new HashMap<>();
// The elements are inserted using the put() method.
m.put(10, "Ali");
m.put(30, "Adam");
m.put(20, "Lily");
//Using for loop for traversal
for (Map.Entry<Integer, String> loop : m.entrySet())
System.out.println("The Key is : " + loop.getKey()
+ " The Value is : " + loop.getValue());
}
}
In the above code:
- The required packages are imported.
- The next step in Java code involves a Class declared as Traverasalinhashmap.
- The entries are added using the put() method of Java HashMap.
- The for loop iterates over the entries to fetch the key and the values.
Output
The below output shows that the key in the first case, i.e., “20” along with its value that is “Lily” is printed using the for loop.

This brings us an end to the implementation of HashMaps in Java.
Conclusion
To create a HashMap in Java, a key is declared and the value is specified to that key. Whenever there is a need to retrieve the data from the HashMap, the key is passed and the value is returned according to the key. In this write-up, we have demonstrated the creation of HashMap using different constructors and also how basic operations like addition, removal, and traversal work in HashMap.