How to Iterate Hashmap in java, As we all know hashmap is a collection class which extends the map interface. Generally, We use the Iterator for iterating over the collections object but in the case of a map, we can not use Iterators directly. Because the map Interface is not a part of the collection. All map classes in java like TreeMap, HashMap, and LinkedHashMap implement Map Interface.
HashMap provided data in the form of (key, value) pair. basically, We can only insert one null key in the hashmap but we can have multiple null values in the hashMap. moreover, If you Inserting data with the same key. It will override the value for the given key using the Hash Function.

There are multiple ways from which we can Iterate over hashMap. In this article, we are going to explain it.
1. Iterate Hashmap in java 8 using stream API
Stream API is a feature of java 8. Stream API in java is used to process the collection Objects.
A stream in java is a sequence of objects that support various methods which can be pipelined to produce the desired result.
Here we will use entrySet().stream for getting the Stream from the Collection data to perform operations. We will use the ForEach() method to Iterate over the HashMap inputs. let’s see the program…
Example 1
//this is program to Iterate over HashMap
// Iterate Over HashMap using Stream API
//Now Importing some packages
// package named 'java.util'
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
// HashMap class
public class HubSpotingHashMap {
//Main method
public static void main(String[] arguments)
{
// Create a Empty HashMap
Map<Integer, String> map
= new HashMap<Integer, String>();
// Now Inseart the Data In HashMap
map.put(1, "Ram");
map.put(2, "Shyam");
map.put(3, "Mahesh");
map.put(4, "Shuresh");
map.put(5,"munna");
// Now Iterate Over Every Entry Set of Hash map and print All data using ForEach Loop
map.entrySet().stream()
.forEach(obj -> System.out.println(obj.getKey() + " : "
+ obj.getValue()));
}
}
// OUTPUT
1 : Ram
2 : Shyam
3 : Mahesh
4 : Shuresh
5 : munna
2. Iterate Hashmap in java using Lambda expression
Here we will Cover How we can Iterating Map in Java 8 using Lambda expression As we have seen Iteration through Stream API …
Basically, Lambda Expression is a block of Code which take inputs as a parameter and provides or returns a value. It is a type of Expression which have no name or return type. we can use it in the body of the method.
The Syntax of the lambda expression is (Simple Form) -:
parameter -> expressions
Example 1
package com.hubspoting;
import java.util.HashMap;
import java.util.Map;
public class HashMapIterationUsingLambda{
public static void main(String[] args) {
Map<String, Integer> map= new HashMap<>();
map.put("abc", 213);
map.put("dfd", 896);
map.put("fgdfg", 466);
map.put("ettrr", 6565);
map.put("ojoj", 313);
/* Iterate of HashMap without using Lambda
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println("Data key: " + entry.getKey() + ", Data value: " + entry.getValue());
}
*/
// iteration Of hashMap with Using Lambda Expression
prices.forEach((k,v)->System.out.println("Data key: " + k + ", Data value: " + v));
}
}
//OUTPUT
Data key:abc , Data value:213
Data key:dfd , Data value:896
Data key:fgdfg , Data value:466
Data key:ettrr , Data value:6565
Data key:ojoj , Data value:313
Example 2
import java.util.HashMap;
import java.util.Map;
// Class
public class IterateHashMapUsingLambda {
// Main driver method
public static void main(String[] args)
{
// Creating hash map
Map<Character, String> charMap
= new HashMap<Character, String>();
// Inserting elements key-value pairs in the hash map ( Custom inputs)
charMap.put('A', "Ram");
charMap.put('B', "Mac");
charMap.put('C', "Doe");
charMap.put('D', "jessy");
// Iterating through forEach and
printing the elements
charType.forEach(
(key, value)
-> System.out.println(key + " = " + value));
}
}
//OUTPUT
A = Ram
B = Mac
C = Doe
D = jessy
3. Iterate Hashmap in java using a For Loop – java hashmap iteration
We can also Iterate Through a map using the For Loop. For this, we have to use methods and we will use entrySet() to get the map View from the HashMap data.
- getValue()
- getKey()
import java.util.HashMap;
import java.util.Map;
// Class for iterating HashMap using for loop
public class HashMapUsingForLoop{
// Main driver method
public static void main(String[] args)
{
// Creating a HashMap
Map<String, String> map
= new HashMap<String, String>();
// Inserting elements to the adobe HashMap
Elements- Key value pairs using put() method
map.put("A", "Angular");
map.put("J", "Java");
map.put("P", "Python");
map.put("H", "Hibernate");
// Iterating HashMap through for loop
for (Map.Entry<String, String> set :
map.entrySet()) {
System.out.println(set.getKey() + " = "
+ set.getValue());
}
}
}
//OUTPUT
P = Python
A = Angular
H = Hibernate
J = Java
Truth behind FREE Grammarly plagiarism checker — check Now
4. Iterate Hashmap in java using ForEach Method – hashmap java iterate
Java 8 provides ForEach() method to Iterate through Collection Object. So we Can use the ForEach method to Iterate HashMap.
// Java Program to Iterate over HashMap
Iterating HashMap using forEach
// Importing Map and HashMap classes
from package names java.util
import java.util.HashMap;
import java.util.Map;
public class HashMapIterationUsingForEach {
public static void main(String[] args)
{
// Creating hash map
Map<Character, String> map
= new HashMap<Character, String>();
// Inserting data in the hash map.
map.put("Q", "Cricket");
map.put("S", "Football");
map.put("H", "Hockey");
map.put("R", "BaseBall");
// Iterating HashMap through forEach and
// Printing all. elements in a Map
charType.forEach(
(key, value)
-> System.out.println(key + " = " + value));
}
}
//OUTPUT
Q = Cricket
S = Football
H = Hockey
R= BaseBall
5. Iterate Hashmap in java Using an Iterator – java iterator hashmap
// Importing classes from java.util package
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class HashMapWithIterator{
public static void main(String[] arguments)
{
// Creating Hash map
Map<Integer, String> map
= new HashMap<Integer, String>();
// Inserting data(Key-value pairs) in hashmap
map.put("Apple", 50);
map.put("Orange", 20);
map.put("Banana", 10);
map.put("Grapes", 40);
map.put("Papaya", 50);
// Iterator
Iterator<Entry<Integer, String> > map_Iterator
= map.entrySet().iterator();
// Iterating every set of entry in the HashMap
while (new_Iterator.hasNext()) {
Map.Entry<Integer, String> new_Map
= (Map.Entry<Integer, String>)
new_Iterator.next();
// Displaying HashMap
System.out.println(new_Map.getKey() + " = "
+ new_Map.getValue());
}
}
}
//OUTPUT
Apple = 50
Orange = 20
Banana = 10
Grapes = 40
Papaya = 50
// Iterating HashMap through for loop for
(Map.Entry<String, String> set : map.entrySet()) {
System.out.println(set.getKey() + ” = ” + set.getValue());
}
how to iterate through a hashmap in java?
how to iterate through a hashmap in java | how to iterate over a hashmap in java
1. Iterate Hashmap in java 8 using stream API
2. Iterate through a HashMap Using an Iterator – java iterator hashmap
3. Iterate Through a HashMap using ForEach Method – hashmap java iterate
4. Iterate through a HashMap using a For Loop – java hashmap iteration
5. Iterating Map in Java 8 using Lambda expression
Also Read
- What companies are in the consumer services field – top 30 list
- Java Quiz 1
- Revealed Instapreneur Secrets
- Truth behind FREE Grammarly plagiarism checker
Final Verdict
This is all from my side! Look at these blog’s to ensure you’re on the right path to your digital success.
Look at these services you may need for your website.
Do you have any questions
Drop your queries and comments section below, or post them on HubSpoting Forums, and we will be happy to answer.
Disclaimer:
Our content is reader-supported and friendly. This means if you click on any one of our links, then we may earn a commission. We are so happy that you support us.