SortedMap Interface in Java with Example

SortedMap is the child interface of Map. If we want to represent a group of key/values pairs according to some sorting order of keys than we should go for SortedMap in Java. Sorting is based on the keys but not value. Sorted maps allow very efficient manipulations of submaps(subsets of a map). To obtain a subset of a map we can use headMap(), tailMap(), or subMap(). To get the first key and the last key we can use firstKey(), lastKey() respectively. A NoSuchElementexception is thrown when no items are in the invoking map, A ClassCastException is thrown when an object is incompatible with the elements in the map i.e. if we use heterogeneous keys, A NullPointerException is thrown when we insert a null element in the map, A IllegalArgumentException is thrown if invalid arguments we used.


Java SortedMap


Methods of SortedMap in Java:

The SortedMap defines the following specific methods.
1. Object firstKey(): This method is used to returns the first key in the invoking map.
2. Object lastKey(): This method is used to returns the last key in the invoking map.
3. SortedMap subMap(Object start, Object end): This method returns a map containing those entries with keys that are greater than or equal to start and less than the end.
4. SortedMap headMap(Object end): This method returns a sorted map for those entries with keys that are less than the end.
5. SortedMap tailMap(Object start): This method returns a sorted map whose entries with keys that are greater than or equal to start.
6. Comparator comparator(): This method returns the invoking sorted map comparator. It returns null if the natural ordering is used for the invoking map.

Java SortedMap Example:


import java.util.TreeMap;
import java.util.SortedMap;

class SortedSetExample{
 public static void main(String args[]){
  
  //creating a sorted map
  SortedMap<Integer, String> smap = new TreeMap<Integer, String>();
  
  //adding values in this map
  smap.put(101, "Rahul");
  smap.put(107, "Amit");
  smap.put(103, "Vijay");
  smap.put(105, "Suresh");
  smap.put(102, "John");
  smap.put(106, "Prashant");
  
  System.out.println(" Sorted Map entries are: "+smap);
  
  //returns first key in the invoking map
  System.out.println("Map first key is: "+smap.firstKey());
  
  //returns last key in the invoking map
  System.out.println("Map last key is: "+smap.lastKey());
  
  /*returns a sorted map for those entries 
  with keys that are less than the end.*/
  System.out.println("Head map is: "+smap.headMap(103));
  
  /*returns a sorted map whose entries with 
  keys that are greater than or equal to the specified key.*/
  System.out.println("Tail map is: "+smap.tailMap(102));
  
  //It returns subset of a map
  System.out.println("Sub map is: "+smap.subMap(103,106));
  
 }
}

Output
Java SortedMap with Example


You may also enjoy these collection articles

ArrayDeque in Java with Example
How to use Queue in Java with Example?
Comparable vs comparator in Java with Example
TreeSet in Java with Example
How to sort a set in java with an example?
How to implement a stack data structure in Java?
How to implement a LinkedList data structure in Java?
How to add or remove an element in the ArrayList?

SortedMap Interface in Java with Example SortedMap Interface in Java with Example Reviewed by Prashant Srivastava on March 20, 2019 Rating: 5

No comments:

Powered by Blogger.