Iterate through Hashmap Java
In this post, you will learn how to iterate through Hashmap in the Java programming language.
Java Hashmap
Java HashMap class is found in the java.util package. Java HashMap class implements the Map interface, which allows us to store key and value pairs where keys should be unique. HashMap in Java is like the legacy Hashtable class, but it is not synchronized. It allows us to store the null elements as well, but there should be only one null key. The HashMap is non-synchronised and not thread safe. Java HashMap contain only unique keys. It is much faster in terms of retrieving data as compared to an array and linked-list.
There are different ways in Java to iterate through its keys, values, and key/value mappings.
Iterate through Hashmap in Java using a for-each loop
The forEach() is a new method introduced in Java 8 to iterate over collections. The HashMap can be iterated over with the forEach() method.
import java.util.HashMap;
import java.util.Map;
public class DisplayHashMap{
public static void main(String[] args) {
Map<Integer, String> item = new HashMap<>();
item.put(5, "Notebook");
item.put(9, "Pen");
item.put(3, "Pencil");
item.put(2, "Eraser");
item.put(1, "Sharpener");
item.forEach((key,value) -> System.out.println(key + " = " + value));
}
}
Output of the above code:
1 = Sharpener
2 = Eraser
3 = Pencil
5 = Notebook
9 = Pen
Iterate through Hashmap in Java using while loop with iterator()
The given Java program uses a while loop with an iterator method of Collection class to iterate through the HashMap.
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
public class DisplayHashMap{
public static void main(String[] args) {
HashMap<Integer, String> item = new HashMap<>();
item.put(5, "Notebook");
item.put(9, "Pen");
item.put(3, "Pencil");
item.put(2, "Eraser");
item.put(1, "Sharpener");
// iterate through keys
Iterator<Integer> itr1 = item.keySet().iterator();
System.out.print("Keys: ");
while(itr1.hasNext()) {
System.out.print(itr1.next());
System.out.print(", ");
}
// iterate through values
Iterator<String> itr2 = item.values().iterator();
System.out.print("Values: ");
while(itr2.hasNext()) {
System.out.print(itr2.next());
System.out.print(", ");
}
}
}
Output of the above code:
Keys: 1, 2, 3, 5, 9,
Values: Sharpener, Eraser, Pencil, Notebook, Pen,
Iterate through Hashmap in Java using for loop
The given Java program uses a for loop to iterate through the HashMap.
import java.util.*;
public class DisplayHashMap{
// Main driver method
public static void main(String[] args)
{
// creating an empty HashMap of integer and string
HashMap<Integer, String> item
= new HashMap<Integer, String>();
// Adding mappings to HashMap
// using put() method
item.put(5, "Notebook");
item.put(9, "Pen");
item.put(3, "Pencil");
item.put(2, "Eraser");
item.put(1, "Sharpener");
// Looping through the HashMap
// Using for loop
for (Map.Entry<Integer, String> mapElement : item.entrySet()) {
int key = mapElement.getKey();
String value = mapElement.getValue();
System.out.println(key + " : " + value);
}
}
}
Output of the above code:
1 : Sharpener
2 : Eraser
3 : Pencil
5 : Notebook
9 : Pen
Related Articles
Number pattern programs in JavaJava program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number