RosettaCodeData/Task/Associative-array-Iteration/Java/associative-array-iteration-2.java

16 lines
402 B
Java
Raw Normal View History

2017-09-23 10:01:46 +02:00
Map<String, Integer> map = new HashMap<>();
map.put("hello", 1);
map.put("world", 2);
map.put("!", 3);
2015-02-20 00:35:01 -05:00
// iterating over key-value pairs:
2017-09-23 10:01:46 +02:00
map.forEach((k, v) -> {
2015-02-20 00:35:01 -05:00
System.out.printf("key = %s, value = %s%n", k, v);
});
// iterating over keys:
2017-09-23 10:01:46 +02:00
map.keySet().forEach(k -> System.out.printf("key = %s%n", k));
2015-02-20 00:35:01 -05:00
// iterating over values:
2017-09-23 10:01:46 +02:00
map.values().forEach(v -> System.out.printf("value = %s%n", v));