View RSS Feed

Algorithm and Data Structure

Java's SortedMap Interface

Rate this Entry
by , 02-19-2012 at 08:38 AM (989 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 operations
Sorted map creation is explained here.
SortedMap mapA = new TreeMap();
Comparator comparator = new MyComparator();
SortedMap mapB = new TreeMap(comparator);

Submit "Java's SortedMap Interface" to Facebook Submit "Java's SortedMap Interface" to Digg Submit "Java's SortedMap Interface" to Submit "Java's SortedMap Interface" to StumbleUpon Submit "Java's SortedMap Interface" to Google