SortedMap is an interface in collection framework. This interface extends Map inrerface and provides a total ordering of its elements (elements can be traversed in sorted order of keys). Exampled class that implements this interface is TreeMap.

The main characteristic of a SortedMap is that, it orders the keys by their natural ordering, or by a specified comparator. So consider using a TreeMap when you want a map that satisfies the following criteria:
- null key or null value are not permitted.
- The keys are sorted either by natural ordering or by a specified comparator.
Methods of SortedMap:
- subMap(K fromKey, K toKey): Returns a view of the portion of this Map whose keys range from fromKey, inclusive, to toKey, exclusive.
- headMap(K toKey): Returns a view of the portion of this Map whose keys are strictly less than toKey.
- tailMap(K fromKey): Returns a view of the portion of this Map whose keys are greater than or equal to fromKey.
- firstKey(): Returns the first (lowest) key currently in this Map.
- lastKey(): Returns the last (highest) key currently in this Map.
- comparator(): Returns the Comparator used to order the keys in this Map, or null if this Map uses the natural ordering of its keys.
- values(): Returns a Collection view of the values contained in this map.
- keySet(): Returns a Set view of the keys contained in this map.
- entrySet(): Returns a Set view of the mappings contained in this map.
Code for SortedMap:
public interface SortedMap extends Map
{
Comparator comparator();
SortedMap subMap(K fromKey, K toKey);
SortedMap headMap(K toKey);
SortedMap tailMap(K fromKey);
K firstKey();
K lastKey();
}
// Java code to demonstrate SortedMap import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; public class SortedMapExample { public static void main(String[] args) { SortedMap<Integer, String> sm = new TreeMap<Integer, String>(); sm.put(new Integer(2), "practice"); sm.put(new Integer(3), "quiz"); sm.put(new Integer(5), "code"); sm.put(new Integer(4), "contribute"); sm.put(new Integer(1), "geeksforgeeks"); Set s = sm.entrySet(); // Using iterator in SortedMap Iterator i = s.iterator(); // Traversing map. Note that the traversal // produced sorted (by keys) output . while (i.hasNext()) { Map.Entry m = (Map.Entry)i.next(); int key = (Integer)m.getKey(); String value = (String)m.getValue(); System.out.println("Key : " + key + " value : " + value); } } } |
Output:
Key : 1 value : geeksforgeeks Key : 2 value : practice Key : 3 value : quiz Key : 4 value : contribute Key : 5 value : code
This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- SortedMap entrySet() method in Java with Examples
- SortedMap keySet() method in Java with Examples
- SortedMap values() method in Java with Examples
- SortedMap comparator() method in Java with Examples
- Java 8 | LongSupplier Interface with Examples
- ToIntFunction Interface in Java with Examples
- LongToIntFunction Interface in Java with Examples
- ToLongFunction Interface in Java with Examples
- DoubleConsumer Interface in Java with Examples
- LongConsumer Interface in Java with Examples
- Java 8 | IntSupplier Interface with Examples
- Java 8 | BooleanSupplier Interface with Examples
- Java 8 | DoubleSupplier Interface with Examples
- IntConsumer Interface in Java with Examples
- SortedSet Interface in Java with Examples



