Java's SortedMap Interface
by, 02-19-2012 at 08:38 AM (729 Views)
The java.util.Map interfaceís subtype is the java.util.SortedMap. Functionality of java.util.Map is extended by this interface and elements are sorted which have been stored in the Map, internally.
Elementís sorting orders are as following:
- If java.lang.Comparable is implemented then it means that there exist elementís natural sorting order.
- Comparator determines the order which you will be providing to the SortedSet.
Default sorting orders are ascending where iteration get started from smallest and moves forward towards largest, in data set. However elements could also be iterated in descending orders. For elements to be iterated in descending order one shall be using TreeMap.descendingKeySet() method.
The java.util.TreeMap is just the Java Collection APIís SortedMap interface. The java.util.concurrent package possess this interfaceís implementation.
Java Code: This is the code to explain map operationsSorted map creation is explained here. SortedMap mapA = new TreeMap(); Comparator comparator = new MyComparator(); SortedMap mapB = new TreeMap(comparator);