Image

Java - Core Java - Collection Framework - SortedMap

SortedMap (I)

  • It is a child interface of Map. If we want to represent a group of (key, value)pairs according to some sorting order of keys then we should go for SortedMap.
  • Sorting is based on the key but not based on the value.
  • SortedMap defines the following specific methods.
  • Program
    import java.util.Map;
    import java.util.SortedMap;
    import java.util.TreeMap;
    public class SortedMapDemo {
        public static void main(String[] args) {
            SortedMap sm=new TreeMap();
          sm.put(101, "A");
          sm.put(103, "B");
          sm.put(104, "C");
          sm.put(107, "D");
          sm.put(125, "E");
          sm.put(136, "F");
            System.out.println(sm);
            for (Map.Entry m : sm.entrySet())        {
                System.out.println(m.getKey()+" "+m.getValue());
            }
            System.out.println(sm.firstKey());
            System.out.println(sm.lastKey());
            System.out.println(sm.headMap(107));
            System.out.println(sm.tailMap(107));
            System.out.println(sm.subMap(103, 125));
            System.out.println(sm.comparator());
        }  
    }
    O/P
    {101=A, 103=B, 104=C, 107=D, 125=E, 136=F}
    101 A
    103 B
    104 C
    107 D
    125 E
    136 F
    101
    136
    {101=A, 103=B, 104=C}
    {107=D, 125=E, 136=F}
    {103=B, 104=C, 107=D}
    null

    NavigableMap

  • The java.util.NavigableMap interface is a subtype of java.util.SortedMap interface.It has few extensions to the SortedSet which makes it possible to navigate the map.
  • The java.util package has only one implementation of NavigableMap interface:java.util.TreeMap.There is an implementation in the java.util. Concurrent package but that is outside of the scope of this trail.
  • Program
    import java.util.*;
    public class NavigableMapDemo 
    {
        public static void main(String[] args) 
    {
            TreeMap tm=new TreeMap();
            tm.put("b", "banana");
            tm.put("c", "cucumber");
            tm.put("a", "apple");
            tm.put("g", "grape");
            tm.put("m", "mango");
            System.out.println(tm);
            System.out.println(tm.ceilingKey("c"));
            System.out.println(tm.higherKey("e"));
            System.out.println(tm.floorKey("e"));
            System.out.println(tm.lowerKey("e"));
            System.out.println(tm.pollFirstEntry());
            System.out.println(tm.pollLastEntry());
            System.out.println(tm.higherEntry("g"));
            System.out.println(tm.lowerEntry("c"));
            System.out.println(tm.ceilingEntry("c"));
            System.out.println(tm.floorEntry("e"));
            System.out.println(tm.descendingKeySet());
            System.out.println(tm.descendingMap());
        }  
    }
    O/P
    {a=apple, b=banana, c=cucumber, g=grape, m=mango}
    c
    g
    c
    c
    a=apple
    m=mango
    null
    b=banana
    c=cucumber
    c=cucumber
    [g, c, b]
    {g=grape, c=cucumber, b=banana}
    

    TreeMap

  • The underlying data structure is RED-BLACK tree.
  • Insertion order is not preserved and it is based on some sorting order of keys.
  • Duplicate keys are not allowed, but values can be duplicated.
  • If we are depending on default natural sorting then keys should be homogeneous and comparable otherwise we will get Runtime Exception saying ClassCastException.If we define our own sorting by comparator then keys need not be homogeneous and comparable. We can take heterogeneous and non comparable object also.
  • Whether we are depending on default natural sorting or customized sorting order, there are no restrictions for values. We can take heterogeneous and non-comparable objects also.
  • Null Acceptance :
  • 1) t.put(null,"x"); RE-NullPointerException
      2) t.put(null,"A");
  • For non-empty TreeMap, if we are trying to insert an entry with null key than we will get runtime error i.e. NullPointerException.
  • For every TreeMap as a first entry with null key is allowed, but after inserting that entry, if we are trying to insert any other entry then we will get runtime exception i.e. NullPointerException.
  • Program:
    import java.util.*;
    public class TreeMapDemo {
        public static void main(String[] args) {
            // TODO code application logic here
            TreeMap tm=new TreeMap(new MyComparator());
            tm.put(100, "ZZZ");
            tm.put(103, "YYY");
            tm.put(101, "XXX");
            tm.put(104, 106);
            System.out.println(tm);
        }
    }
    // For customizedsorting
    class MyComparator implements Comparator
    {
        public int compare(Object obj1,Object obj2)
        {
            String s1=obj1.toString();
            String s2=obj2.toString();
            return s2.compareTo(s1);
        }
    }
    O/P
    {104=106, 103=YYY, 101=XXX, 100=ZZZ}