
Iterate Map in Java: You will learn how to iterate over a Map in Java and the various iteration methods, Java Program to Iterate over a HashMap in this article. First let’s understand about Map in Java.
Map in Java stores elements in the form of key and value pairs. If the key is provided then map returns corresponding value.
The same method of iteration will work for any Map implementation, including HashMap, TreeMap, LinkedHashMap, Hashtable, etc., because all Java Maps implement the Map interface.
Table of Contents
5 different ways to iterate a Map in Java
There are following ways to iterate map in Java:
- Map.entrySet() with For-Each loop
- keySet() and values()
- forEach(action) method
- Searching of keys in map
- By using iterators
Iterate Map using Map.entrySet() with For-Each loop
In this method of iteration, we use Map.Entry<K,V> in which Entry means key value pair. We get a collection-view of the map from Map.entrySet(), whose elements are in this class.
import java.util.Map;
import java.util.HashMap;
public class GeekCer {
public static void main(String[] arg) {
Map<String,String> map = new HashMap<String,String>();
// Add data as key-key value pair
map.put("ADAMS", "CLERK");
map.put("BLAKE", "MANAGER");
map.put("FORD", "ANALYST");
map.put("MILLER", "CLERK");
map.put("TURNER", "SALESMAN");
for (Map.Entry entry : map.entrySet()) {
System.out.println("Key = " + entry.getKey() +
", Value = " + entry.getValue());
}
}
}
Output: Key = TURNER, Value = SALESMAN Key = BLAKE, Value = MANAGER Key = MILLER, Value = CLERK Key = ADAMS, Value = CLERK Key = FORD, Value = ANALYST
Iterate Map using keySet() and values()
In this program, we have iterated over keys using keySet() method and iterated over values using values() method. The keySet() method returns a set view of the keys contained in this map and values() method returns of the values contained in this map.
import java.util.Map;
import java.util.HashMap;
public class GeekCer {
public static void main(String[] arg) {
Map<String,String> map = new HashMap<String,String>();
// Add data as key-key value pair
map.put("ADAMS", "CLERK");
map.put("BLAKE", "MANAGER");
map.put("FORD", "ANALYST");
map.put("MILLER", "CLERK");
map.put("TURNER", "SALESMAN");
// Iteration over keys using keySet()
for (String name : map.keySet()) {
System.out.println("Key: " + name);
}
// Iteration over values using values()
for (String job : map.values()) {
System.out.println("Value: " + job);
}
}
}
Output: Key: TURNER Key: BLAKE Key: MILLER Key: ADAMS Key: FORD Value: SALESMAN Value: MANAGER Value: CLERK Value: CLERK Value: ANALYST
Iterate map in java Using forEach(action) method
This technique available in Java 8, here we iterate a map using lambda expression and forEach(action) method.
import java.util.Map;
import java.util.HashMap;
public class GeekCer {
public static void main(String[] arg) {
Map<String,String> map = new HashMap<String,String>();
// Add data as key-key value pair
map.put("ADAMS", "CLERK");
map.put("BLAKE", "MANAGER");
map.put("FORD", "ANALYST");
map.put("MILLER", "CLERK");
map.put("TURNER", "SALESMAN");
map.forEach((key ,value) -> System.out.println("Key = "
+ key + ", Value = " + value));
}
}
Output: Key = TURNER, Value = SALESMAN Key = BLAKE, Value = MANAGER Key = MILLER, Value = CLERK Key = ADAMS, Value = CLERK Key = FORD, Value = ANALYST
Searching of keys in map
It a simple way of iteration, firstly we get the key from the map and by using key we get the value from the map.
import java.util.Map;
import java.util.HashMap;
public class GeekCer {
public static void main(String[] arg) {
Map<String,String> map = new HashMap<String,String>();
// Add data as key-key value pair
map.put("ADAMS", "CLERK");
map.put("BLAKE", "MANAGER");
map.put("FORD", "ANALYST");
map.put("MILLER", "CLERK");
map.put("TURNER", "SALESMAN");
for (String name : map.keySet()) {
String job = map.get(name);
System.out.println("Key = " + name + ", Value = " + job);
}
}
}
Output: Key = TURNER, Value = SALESMAN Key = BLAKE, Value = MANAGER Key = MILLER, Value = CLERK Key = ADAMS, Value = CLERK Key = FORD, Value = ANALYST
Iterate map in java using iterators
First of all, we create a iterator for the collection-view returned by the entrySet() method and using while loop we iterate the map.
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class GeekCer {
public static void main(String[] arg) {
Map<String,String> map = new HashMap<String,String>();
// Add data as key-key value pair
map.put("ADAMS", "CLERK");
map.put("BLAKE", "MANAGER");
map.put("FORD", "ANALYST");
map.put("MILLER", "CLERK");
map.put("TURNER", "SALESMAN");
Iterator<Map.Entry<String, String>> itr = map.entrySet().iterator();
while(itr.hasNext())
{
Map.Entry<String, String> entry = itr.next();
System.out.println("Key = " + entry.getKey() +
", Value = " + entry.getValue());
}
}
}
Output: Key = TURNER, Value = SALESMAN Key = BLAKE, Value = MANAGER Key = MILLER, Value = CLERK Key = ADAMS, Value = CLERK Key = FORD, Value = ANALYST
More about collection
We can not store primitive dat types in the collection objects hence map doesn’t store the primitive type of data. The main purpose of collection is to handle object only, but not primitive data types.
Can you store a primitive data type into a collection?
No, Collections store the elements in the form of object only. But you can store a primitive data in the form of object by using wrapper class concept.
Recommended Articles
- Benefits of lambda expressions in java
- Java Tutorials for Experienced
- DBMS characteristics in Hindi
- Java 8 map() function in stream