The HashMaps in Java are used to store the (key, value) pair and these can be accessed using the index value declared as integers. The keys in the HashMap is a unique entity that specifies the values mapped to it. If a duplicate key is inserted the elements are replaced corresponding to that key. Since the key is a unique entity we can perform operations like addition, updation, removal, and iteration through the key.
In this article, we will implement various methods to implement the process of iteration in HashMap in Java.
How to Iterate over a HashMap in Java?
HashMap in Java implements different functions according to the needs of the users. One of the operations is to iterate/visit each element in the HashMap is known as “iteration”. The word “iteration” refers to the “looping through the elements” present in a HashMap in Java. Each element in the HashMap can be visited using the iterator. There are certain methods to iterate over a HashMap in Java that are:
- for Loop to iterate over HashMap.
- Implementing an iterator to Iterate on a HashMap
- Iteration of HashMap using Loop with Stream API
- Iteration on HashMap using Lambda Expressions
Below is the implementation of different methods for iterating over a HashMap in Java.
Method 1: Using for Loop to Iterate the Hashmap
The method below uses the for loop with getValue() and getKey() to iterate over a HashMap.
//Import the required packages
import java.util.HashMap;
import java.util.Map;
//Declare class for iterator
public class iterator {
public static void main(String[] args)
{
Map<Integer, String> a = new HashMap<Integer, String>();
a.put(1, "Iteration");
a.put(2, "in");
a.put(3, "Java");
System.out.println("Iteration Using for Loop");
//for loop iteration on Hashmap
for (Map.Entry<Integer, String> set :a.entrySet()) {
//Printing the key and value using the getKey() method
System.out.println(set.getKey() + " : "+ set.getValue());
}
}
}
In the above Java code:
- The required HashMap packages are imported.
- A class named as “iterator” is declared.
- The put() method adds the Hashmap’s key and value.
- The next step involves the iteration through the for loop.
- The println() method of Java prints the key and the respective values for the keys.
Output
The below output depicts that the keys that are 1, 2, and 3 are printed along with the values that are Iteration, in, and Java.

Method 2: Iterator to Iterate Through the HashMap
The below code block describes the implementation of the iterator using the While loop and iterator() method.
//Import the required packages
import java.util.*;
import java.util.Map;
import java.util.Map.Entry;
//Declare the class for the iterator method
public class iterator {
public static void main(String[] arguments)
{
Map<Integer, String> inte= new HashMap<Integer, String>();
// Inserting data(Key-value pairs) in hashmap
inte.put(1, "Python");
inte.put(2, "Programming");
System.out.println("Iteration Using iterator");
Iterator<Entry<Integer, String> > new_Iterator= inte.entrySet().iterator();
while (new_Iterator.hasNext()) {
Map.Entry<Integer, String> new_Map= (Map.Entry<Integer, String>)
new_Iterator.next();
//Print the output
System.out.println(new_Map.getKey() + " : "
+ new_Map.getValue());
}
}
}
In the above Java code:
- The required packages are imported for using the iterator to iterate.
- The key and the respective values are inserted using the put() method.
- The while loop iterates through every set of entries using the hasNext() declared in the HashMap.
- The getKey() and getValue() method of Java fetches the respective keys and the values.
Output
The below output depicts that the keys 1 and 2 are printed with their values “Python” and “Programming” using the iterator method.

Method 3: Iteration Using Loop With Stream API
The entrySet.stream() method is used in the code below to iterate over the HashMap in Java.
import java.util.*;
//Declare class to iterate using API Stream
public class streamiterator {
public static void main(String[] arguments)
{
Map<Integer, String> i= new HashMap<Integer, String>();
i.put(11, "Welcome");
i.put(12, "To");
i.put(13, "Java");
i.put(14, "Programming");
System.out.println("Iteration using the Stream API");
//Iteration on each element in the HashMap and print the key and value
i.entrySet().stream().forEach(input-> System.out.println(input.getKey() + " = "+ input.getValue()));
}
}
In the above code:
- The first step involves a declaration of the class as “streamiterator”.
- A HashMap is declared with Integer as keys and Strings as values for iteration.
- The stream() method is responsible for returning the stream object.
- foreach() method is responsible for iterating over input objects in the entrySet().
- The output is printed using the println() method.
Output
The below output depicts that the key along with the values are printed using the Stream API method.

Method 4: Iteration Using Lambda Expression
The lambda expressions in Java are responsible for taking the parameters and returning the value with respect to those parameters. The main advantage of using the Lambda expression is that it does not require a name and can be declared inside the body of a method. Below is the code implementation of iteration in Java using the lambda expressions:
import java.util.*;
public class lambdaexp {
public static void main(String[] args)
{
HashMap<String, Integer> h = new HashMap<>();
//Create HashMap using the put() method
h.put("Python", 1);
h.put("Java", 2);
h.put("C++", 3);
//the foreach() method to iterate over the HashMap and print the values
h.forEach((key, value) -> System.out.println(key + ":" + value));
}
}
In the above code:
- The java.util package is imported.
- A class is declared as “lambdaexp” to perform iteration.
- The next step involves the addition of strings and integers using the put() method.
- forEach() method performs the iteration over the HashMap and prints the output using the println() method.
Output
In the output below, the HashMap is printed, it is to keep in mind that the HashMap may appear in an unsorted order since it is not responsible for sorting like in arrays and lists.

Conclusion
The iteration in HashMap refers to the looping through the elements of the HashMap. Each key, value pair is visited in the HashMap and printed accordingly. There are different methods in Java to iterate over HashMap. This article has effectively implemented various methods for iterating over a Java HashMap.