The entryset method of a Java Map is used to provide a Set “view” of the Map. Since Map
is not iterable, this method provides a way to iterate over the key-value pairs. Let’s look at it in action:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.HashMap;
import java.util.Set;
class Streams {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("hello", "world");
map.put("hola", "mundo");
Set<HashMap.Entry<String, String>> set = map.entrySet();
for (HashMap.Entry<String, String> entry : set) {
System.out.println("Key: " + entry.getKey() + " Value: " + entry.getValue());
}
}
}
The output of this program is:
1
2
Key: hello Value: world
Key: hola Value: mundo
data_structures
java
programming
]