One can iterate through a SortedMap by using the iterator from myMap.entrySet().iterator(). But does this iterator preserve the ordering of the sorted map object ?
Yes. See the javadocs for SortedMap:
This order is reflected when iterating over the sorted map's collection views (returned by the entrySet, keySet and values methods).
The SortedMap interface has no own methods to iterate through the entries. What is the standard way to iterate ordered through the entries ?
The usual trick is to use entrySet()
, and iterate on those Map.Entry
objects. For instance:
for (Map.Entry<K,V> entry : theMap.entrySet()) {
K entryKey = entry.getKey();
V entryValue = entry.getValue();
// do whatever with them
}