Topics Index

Collections Framework > Map Interfaces >
Siva Nookala - 15 Apr 2016
The SortedMap interface extends Map. It ensures that the entries are maintained in ascending key order.

SortedMap Methods :
Comparator comparator()Returns the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.
Set entrySet()Returns a Set view of the mappings contained in this map.
Object firstKey()Returns the first (lowest) key currently in this map.
SortedMap headMap(Object tokey)Returns a view of the portion of this map whose keys are strictly less than toKey.
Set keySet()Returns a Set view of the keys contained in this map.
Object lastKey()Returns the last (highest) key currently in this map.
SortedMap subMap(Object fromKey, Object toKey)Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.
SortedMap tailMap(Object fromkey)Returns a view of the portion of this map whose keys are greater than or equal to fromKey.
Collection values()Returns a Collection view of the values contained in this map.

Sorted Map
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

class SortedMapTest
    public static void main(String arg[])
        TreeMap<String, Double> tm = new TreeMap<String, Double>();
        tm.put("Santosh", new Double(3020.55));
        tm.put("Ram", new Double(2550.22));
        tm.put("Nishan", new Double(2060.66));
        tm.put("Amar", new Double(1890.88));
        tm.put("Om", new Double(1650.11));
        System.out.println("Map after initialization : " + tm);
        Set set = tm.entrySet(); // Setting entry set
        Iterator i = set.iterator(); // Iterating to set
        while (i.hasNext()) {
            // Assigning iterator to map entry
            Map.Entry m = (Map.Entry);
            if (m.getKey().equals("Ram")) {
                m.setValue(3550.33); // Set value for Ram key
            // Getting key and value from map entry
            System.out.println(m.getKey() + " : " + m.getValue());
        System.out.println("Map after changing Ram value : " + tm);
        System.out.println("First key : " + tm.firstKey());
        System.out.println("Last key : " + tm.lastKey());
        System.out.println("Keys set : " + tm.keySet());
        System.out.println("Values set : " + tm.values());
        System.out.println("Head map : " + tm.headMap("Om"));
        System.out.println("Sub map : " + tm.subMap("Nishan", "Santosh"));
        System.out.println("Tail map : " + tm.tailMap("Om"));

Map after initialization : {Amar=1890.88, Nishan=2060.66, Om=1650.11, Ram=2550.22, Santosh=3020.55}
Amar : 1890.88
Nishan : 2060.66
Om : 1650.11
Ram : 3550.33
Santosh : 3020.55
Map after changing Ram value : {Amar=1890.88, Nishan=2060.66, Om=1650.11, Ram=3550.33, Santosh=3020.55}
First key : Amar
Last key : Santosh
Keys set : [Amar, Nishan, Om, Ram, Santosh]
Values set : [1890.88, 2060.66, 1650.11, 3550.33, 3020.55]
Head map : {Amar=1890.88, Nishan=2060.66}
Sub map : {Nishan=2060.66, Om=1650.11, Ram=3550.33}
Tail map : {Om=1650.11, Ram=3550.33, Santosh=3020.55}


In this program, a TreeMap of key type String and value type Double is taken which stores elements in a ascending order. Five elements are added to the tree map, then entry set is assigned to set, set is assigned to iterator, and the iterator elements are assigned to map entry to retrieve the keys and values. Few methods of sorted map are used to retrieve required elements.

  • Add Prakash and 4500.45 into tree map by using put method.
  • In equals method give Amar and set value to 1500.60.
  • Create a new tree map with key type as String i.e. subjects and value type as Integer i.e. marks, add the marks to subjects, this will store the elements in ascending order. Try calling sorted map methods.

Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.