How to Iterate Over Each Element of Map in Java
-
How to Iterate
Map
Elements in Java -
Iterate
Map
Elements Usingfor
Loop in Java -
Iterate
Map
Elements Usingforeach
in Java -
Iterate
Map
Elements UsingEntry
andIterator
in Java -
Iterate
Map
Elements Usingfor-each
andkeySet()
in Java -
Iterate
Map
Elements Usingwhile-loop
in Java -
Iterate
Map
Elements UsingStream
andforEach
in Java -
Iterate Map Elements Using
forEach
andlambda
in Java
This tutorial introduces how to iterate over each element of map and lists some example codes to understand it.
How to Iterate Map
Elements in Java
Map is an interface which is used to collect data in the form of key-value pair. Java provides several ways to iterate map elements such as for
loop, for-each
loop, while
loop, forEach()
method, etc. Let’s see the examples.
Iterate Map
Elements Using for
Loop in Java
We use simple for
loop to iterate the Map
elements. Here, in the loop iterator()
method is used to get entries.
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
for (Iterator<Map.Entry<Integer, String>> entries = map.entrySet().iterator();
entries.hasNext();) {
Map.Entry<Integer, String> entry = entries.next();
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
}
Output:
100 : Hundred
1000 : Thousand
10 : Ten
Iterate Map
Elements Using foreach
in Java
We use for-each
loop and entrySet()
method to iterate each entry of the map. The entrySet()
returns a set of entries of the map.
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
}
Output:
100 : Hundred
1000 : Thousand
10 : Ten
Iterate Map
Elements Using Entry
and Iterator
in Java
The iterator()
method returns an Iterator
to traverse the elements while Entry
is used to collect entry of Map
.
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
Iterator<Map.Entry<Integer, String>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<Integer, String> entry = entries.next();
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
}
Output:
100 : Hundred
1000 : Thousand
10 : Ten
Iterate Map
Elements Using for-each
and keySet()
in Java
The keySet()
method is used to collect set of keys of Map
that further is used to iterate using for-each
loop.
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
for (Integer key : map.keySet()) {
System.out.println(key + " : " + map.get(key));
}
}
}
Output:
100 : Hundred
1000 : Thousand
10 : Ten
Iterate Map
Elements Using while-loop
in Java
Here, we used iterator()
method to get iterator of keys and then iterating these keys using while-loop. To get value by a key, we used get()
method.
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
Iterator<Integer> itr = map.keySet().iterator();
while (itr.hasNext()) {
Integer key = itr.next();
System.out.println(key + " : " + map.get(key));
}
}
}
Output:
100 : Hundred
1000 : Thousand
10 : Ten
Iterate Map
Elements Using Stream
and forEach
in Java
We can use stream to iterate the elements. Here, we used entrySet()
to collect map entries that further traversed through forEach()
method of Stream
.
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
map.entrySet().stream().forEach(System.out::println);
}
}
Output:
100=Hundred
1000=Thousand
10=Ten
Iterate Map Elements Using forEach
and lambda
in Java
We can also use lambda expression to iterate the map elements. Here, we used lambda expression inside forEach()
method.
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
map.forEach((key, value) -> System.out.println(key + " : " + value));
}
}
Output:
100 : Hundred
1000 : Thousand
10 : Ten
Related Article - Java Map
- Increment Map in Java
- How to Convert Stream Element to Map in Java
- How to Convert Map Values Into a List in Java
- How to Convert List to Map in Java
- How to Filter A Value From Map in Java
- How to Create Map in Java