Logo

Programming-Idioms

History of Idiom 112 > diff from v14 to v15

Edit summary for version 15 by programming-idioms.org:
[Java] Fixed generic types

Version 14

2016-12-07, 21:59:28

Version 15

2016-12-07, 21:59:50

Idiom #112 Iterate over map entries, ordered by keys

Print each key k with its value x from an associative array mymap, in ascending order of k.

Idiom #112 Iterate over map entries, ordered by keys

Print each key k with its value x from an associative array mymap, in ascending order of k.

Extra Keywords
traverse traversal
Extra Keywords
traverse traversal
Imports
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
Imports
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
Code
SortedMap<K, V> mymap = new TreeMap<>();
...
for(Map.Entry<Integer, String> e: mymap.entrySet())
	System.out.println("Key=" + e.getKey() + ", Value=" + e.getValue());
Code
SortedMap<K, V> mymap = new TreeMap<>();
...
for(Map.Entry<K, V> e: mymap.entrySet())
	System.out.println("Key=" + e.getKey() + ", Value=" + e.getValue());
Comments bubble
When mymap implements SortedMap, sorted iteration is straightforward.
Comments bubble
When mymap implements SortedMap, sorted iteration is straightforward.
Doc URL
https://docs.oracle.com/javase/8/docs/api/java/util/SortedMap.html
Doc URL
https://docs.oracle.com/javase/8/docs/api/java/util/SortedMap.html
Demo URL
http://ideone.com/bTEfhu
Demo URL
http://ideone.com/bTEfhu