SortedMap in Java

SortedMap

In the last blog, we learned about the class called TreeMap. If you want to learn more about it, visit TreeMap in Java. In this blog, we will go through the interface called SortedMap.


SortedMap

SortedMap interface extends Map. All the entries in the SortedMap are stored in ascending order. As SortedMap is an interface we can use it directly. In order to use the functionalities of the SortedMap, we can use TreeMap which implements the SortedMap. SortedMap is present in java.util package. We can create the SortedMap in the following way:

SortedMap<Key, Value> smap = new TreeMap<>();

Methods of SortedMap

  1. comparator()
  2. The comparator() method produces a comparator that is used to order the keys in the SortedMap, or it returns null if the SortedMap's keys are ordered naturally.

    e.g.

    import java.util.*;
    public class SortedMapComparatorEx {
      public static void main(String[] args)throws Exception{
        try {
          SortedMap<Integer, String>
          sortedMap = new TreeMap<Integer, String>();
          sortedMap.put(101, "George");
          sortedMap.put(102, "Jen");
          sortedMap.put(103, "Kristin");
          sortedMap.put(104, "Donna");
          System.out.println("SortedMap: " + sortedMap);
          Comparator comp = sortedMap.comparator();
          System.out.println("Comparator value: "
                  + comp);
        }catch (NullPointerException e) {
            System.out.println("Exception : " + e);
        }
      }
    }

    Output

    SortedMap: {101=George, 102=Jen, 103=Kristin, 104=Donna}
    Comparator value: null
  3. firstKey()
  4. firstKey() method returns the first key of the sorted map.

    e.g.

    import java.util.*;
    public class SortedMapFirstKeyEx {
      public static void main(String[] args)throws Exception{
        SortedMap<Integer, String>
        sortedMap = new TreeMap<Integer, String>();
        sortedMap.put(101, "George");
        sortedMap.put(102, "Jen");
        sortedMap.put(103, "Kristin");
        sortedMap.put(104, "Donna");
        System.out.println("SortedMap: " + sortedMap);
        System.out.println("First Key of the Sorted Map: "+ sortedMap.firstKey());
      }
    }

    Output

    SortedMap: {101=George, 102=Jen, 103=Kristin, 104=Donna}
    First Key of the Sorted Map: 101
  5. lastKey()
  6. lastKey() method returns the last key of the sorted map.

    e.g.

    import java.util.*;
    public class SortedMapLastKeyEx {
      public static void main(String[] args)throws Exception{
        SortedMap<Integer, String>
        sortedMap = new TreeMap<Integer, String>();
        sortedMap.put(101, "George");
        sortedMap.put(102, "Jen");
        sortedMap.put(103, "Kristin");
        sortedMap.put(104, "Donna");
        System.out.println("SortedMap: " + sortedMap);
        System.out.println("Last Key of the Sorted Map: "+ sortedMap.firstKey());
      }
    }

    Output

    SortedMap: {101=George, 102=Jen, 103=Kristin, 104=Donna}
    Last Key of the Sorted Map: 104
  7. headMap(Object end)
  8. headMap(Object end) returns the SortedMap that contains the elements that are less than the end of the SortedMap.

    e.g.

    import java.util.*;
    public class SortedMapHeadMapEx {
      public static void main(String[] args)throws Exception{
        SortedMap<Integer, String>
        sortedMap = new TreeMap<Integer, String>();
        sortedMap.put(101, "George");
        sortedMap.put(102, "Jen");
        sortedMap.put(103, "Kristin");
        sortedMap.put(104, "Donna");
        System.out.println("SortedMap: " + sortedMap);
        System.out.println("Last key: "
                    + sortedMap.headMap(103));
      }
    }

    Output

    SortedMap: {101=George, 102=Jen, 103=Kristin, 104=Donna}
    Last key: {101=George, 102=Jen}
  9. tailMap(Object start)
  10. tailMap(Object start) returns all entries of the map whose keys are greater than or equal to the specified keys.

    e.g.

    import java.util.*;
    public class SortedMapTailMapEx {
      public static void main(String[] args)throws Exception{
        SortedMap<Integer, String>
        sortedMap = new TreeMap<Integer, String>();
        sortedMap.put(101, "George");
        sortedMap.put(102, "Jen");
        sortedMap.put(103, "Kristin");
        sortedMap.put(104, "Donna");
        System.out.println("SortedMap: " + sortedMap);
        System.out.println("tailMap() result: " + sortedMap.tailMap(103));
      }
    }

    Output

    SortedMap: {101=George, 102=Jen, 103=Kristin, 104=Donna}
    tailMap() result: {103=Kristin, 104=Donna}
  11. subMap(Object start, Object end)
  12. subMap(Object start, Object end) returns the SortedMap containing the elements between the start and end.

    e.g.

    import java.util.*;
    public class SortedMapSubMapEx {
      public static void main(String[] args)throws Exception{
        SortedMap<Integer, String>
        sortedMap = new TreeMap<Integer, String>();
        sortedMap.put(101, "George");
        sortedMap.put(102, "Jen");
        sortedMap.put(103, "Kristin");
        sortedMap.put(104, "Donna");
        System.out.println("SortedMap: " + sortedMap);
        System.out.println("subMap() result: " + sortedMap.subMap(101,104));
      }
    }

    Output

    SortedMap: {101=George, 102=Jen, 103=Kristin, 104=Donna}
    subMap() result: {101=George, 102=Jen, 103=Kristin}
Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.