SortedMap Interface

SortedMap Interface

In this tutorial, we are going to discuss about SortedMap Interface in java. The SortedMap interface in Java is a sub interface of the Map interface and extends it to provide a sorted mapping of keys to values. It guarantees that the keys are maintained in ascending order, according to the natural ordering of its keys or by a specified comparator.

SortedMap interface is particularly useful when you need to maintain the keys in a sorted order and perform operations such as range searches and iteration over the keys in sorted order.

SortedMap Interface
  • SortedMap is the 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 possible only based on the keys but not based on values.
  • It defines the following 6 specific methods.

1. Comparator comparator( )

Produces the comparator used for this Map, or null for natural ordering.

2. Object firstKey( )

Produces the lowest key.

3. Object lastKey( )

Produces the highest key.

4. SortedMap subMap(fromKey, toKey)

Produces a view of this Map with keys from fromKey, inclusive, to toKey, exclusive.

5. SortedMap headMap(toKey)

Produces a view of this Map with keys less than toKey.

6. SortedMap tailMap(fromKey)

Produces a view of this Map with keys greater than or equal to fromKey.

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

public class SortedMapExample {
    public static void main(String[] args) {
        // Creating a TreeMap
        SortedMap<String, Integer> map = new TreeMap<>();

        // Adding key-value pairs
        map.put("Apple", 10);
        map.put("Banana", 20);
        map.put("Orange", 15);

        // Accessing values
        System.out.println("Value for 'Apple': " + map.get("Apple"));

        // Iterating over keys in sorted order
        System.out.print("Keys in sorted order: ");
        for (String key : map.keySet()) {
            System.out.print(key + " ");

        // Getting the first and last keys
        System.out.println("First key: " + map.firstKey());
        System.out.println("Last key: " + map.lastKey());

        // Getting a submap
        SortedMap<String, Integer> subMap = map.subMap("Apple", "Orange");
        System.out.println("Submap (Apple to Orange): " + subMap);

This example demonstrates creating a TreeMap, adding key-value pairs, accessing values, iterating over keys in sorted order, and obtaining submap views within a specified range of keys.

Key Characteristics of SortedMap
  1. Sorted Order: The keys in a SortedMap interface are maintained in sorted order either according to their natural ordering (if they implement the Comparable interface) or by a custom comparator provided during creation.
  2. Efficient Retrieval: SortedMap implementations provide efficient retrieval of keys and their corresponding values based on their sorted order.
  3. Submap Views: SortedMap provides methods to obtain submap views that represent a range of keys within the sorted map.

That’s all about the SortedMap interface in java. If you have any queries or feedback, please write us email at Enjoy learning, Enjoy Java.!!

SortedMap Interface
Scroll to top