EN
Java - iterate over HashMap
13
points
Short solution - using forEach from Java 8
Map<String, Integer> map = new HashMap<>();
map.forEach((key, value) -> {
// use key and value variables
System.out.println("Key: " + key + ", Value : " + value);
});
Short solution - using for loop and entrySet()
Map<String, Integer> map = new HashMap<>();
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println("Key: " + entry.getKey() + ", Value : " + entry.getValue());
}
Short solution - using Iterator
Map<String, Integer> map = new HashMap<>();
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, Integer> entry = (Map.Entry<String, Integer>) iter.next();
System.out.println("Key: " + entry.getKey() + ", Value : " + entry.getValue());
}
Short solution - iterate over keys
Map<String, Integer> map = new HashMap<>();
map.keySet().forEach(key -> {
// use key variable
System.out.println("Key: " + key);
});
Short solution - iterate over values
Map<String, Integer> map = new HashMap<>();
map.values().forEach(value -> {
// use key variable
System.out.println("Value: " + value);
});
1. Using forEach from Java 8
import java.util.HashMap;
import java.util.Map;
public class Example1 {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("A", 10);
map.put("B", 20);
map.put("C", 30);
map.forEach((key, value) -> {
// use key and value variables
System.out.println("Key: " + key + ", Value : " + value);
});
}
}
Output:
Key: A, Value : 10
Key: B, Value : 20
Key: C, Value : 30
2. Using for loop and entrySet()
import java.util.HashMap;
import java.util.Map;
public class Example2 {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("A", 10);
map.put("B", 20);
map.put("C", 30);
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println("Key: " + key + ", Value : " + value);
}
}
}
Output:
Key: A, Value : 10
Key: B, Value : 20
Key: C, Value : 30
3. Using Iterator
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Example3 {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("A", 10);
map.put("B", 20);
map.put("C", 30);
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, Integer> entry =
(Map.Entry<String, Integer>) iter.next();
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println("Key: " + key + ", Value : " + value);
}
}
}
Output:
Key: A, Value : 10
Key: B, Value : 20
Key: C, Value : 30
4. Iterate over keys
import java.util.HashMap;
import java.util.Map;
public class Example4 {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("A", 10);
map.put("B", 20);
map.put("C", 30);
map.keySet().forEach(key -> {
// use key variable
System.out.println("Key: " + key);
});
}
}
Output:
Key: A
Key: B
Key: C
5. Iterate over values
import java.util.HashMap;
import java.util.Map;
public class Example5 {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("A", 10);
map.put("B", 20);
map.put("C", 30);
map.values().forEach(value -> {
// use key variable
System.out.println("Value: " + value);
});
}
}
Output:
Value: 10
Value: 20
Value: 30