SortedMap Interface in Java with Examples

SortedMap is an interface in the collection framework. This interface extends the Map interface and provides a total ordering of its elements (elements can be traversed in sorted order of keys). The exampled class that implements this interface is TreeMap.

sortedmap

The main characteristic of a SortedMap is that it orders the keys by their natural ordering, or by a specified comparator. So consider using a TreeMap when you want a map that satisfies the following criteria:

  • null key or null value is not permitted.
  • The keys are sorted either by natural ordering or by a specified comparator.

Methods of SortedMap:

  1. subMap(K from the key, K toKey): Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.
  2. heatmap(K toKey): Returns a view of the portion of this map whose keys are strictly less than toKey.
  3. trail map(K from key): Returns a view of the portion of this map whose keys are greater than or equal to from key.
  4. firstly(): Returns the first (lowest) key currently in this Map.
  5. Laskey(): Returns the last (highest) key currently in this Map.
  6. comparator(): Returns the Comparator used to order the keys in this Map, or null if this Map uses the natural ordering of its keys.
  7. values(): Returns a Collection view of the values contained in this map.
  8. keySet(): Returns a Set view of the keys contained in this map.
  9. entrySet(): Returns a Set view of the mappings contained in this map.

Code for SortedMap:

public interface SortedMap extends Map
{
    Comparator comparator();
    SortedMap subMap(K fromKey, K toKey);
    SortedMap headMap(K toKey);
    SortedMap tailMap(K fromKey);
    K firstKey();
    K lastKey();
}

filter_none

edit

play_arrow

brightness_4

// Java code to demonstrate SortedMap

import java.util.Iterator;

import java.util.Map;

import java.util.Set;

import java.util.SortedMap;

import java.util.TreeMap;

  

public class SortedMapExample

{

    public static void main(String[] args)

    {

        SortedMap<Integer, String> sm =

                    new TreeMap<Integer, String>();

        sm.put(new Integer(2), "practice");

        sm.put(new Integer(3), "quiz");

        sm.put(new Integer(5), "code");

        sm.put(new Integer(4), "contribute");

        sm.put(new Integer(1), "geeksforgeeks");

        Set s = sm.entrySet();

  

        // Using iterator in SortedMap

        Iterator i = s.iterator();

  

        // Traversing map. Note that the traversal

        // produced sorted (by keys) output .

        while (i.hasNext())

        {

            Map.Entry m = (Map.Entry)i.next();

  

            int key = (Integer)m.getKey();

            String value = (String)m.getValue();

  

            System.out.println("Key : " + key +

                            "  value : " + value);

        }

    }

}

Output:

Key : 1  value : geeksforgeeks
Key : 2  value : practice
Key : 3  value : quiz
Key : 4  value : contribute
Key : 5  value : code

 

 

Related Articles

post a comment