Logo

Programming-Idioms

History of Idiom 112 > diff from v12 to v13

Edit summary for version 13 by programming-idioms.org:
New Java implementation by user [programming-idioms.org]

Version 12

2016-10-18, 21:25:04

Version 13

2016-12-07, 21:45:11

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