Collections Framework - The SortedMap Interface

Overview of SortedMap Interface

SortedMap is a Map that maintains its entries in ascending order, sorted according to the keys natural ordering, or according to a Comparator provided at the time of the SortedMap creation.
The SortedMap interface provides operations for normal Map operations and for the following:
  • Range view — performs arbitrary range operations on the sorted map
  • Endpoints — returns the first or the last key in the sorted map
  • Comparator access — returns the Comparator, if any, used to sort the map
The following interface is the Map analog of SortedSet.
public interface SortedMap<K, V> extends Map<K, V>{
    Comparator<? super K> comparator();
    SortedMap<K, V> subMap(K fromKey, K toKey);
    SortedMap<K, V> headMap(K toKey);
    SortedMap<K, V> tailMap(K fromKey);
    K firstKey();
    K lastKey();
}

SortedMap Interface Example

This example demonstrates the few API of SortedMap Interface using TreeSet implementation class.
  • firstKey()
  • lastKey()
  • tailMap(String fromKey)
  • .headMap(String toKey)
import java.util.SortedMap;

import java.util.TreeMap;

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

        // Adding new key-value pairs to a TreeMap
        fileExtensions.put("python", ".py");
        fileExtensions.put("c++", ".cpp");
        fileExtensions.put("kotlin", ".kt");
        fileExtensions.put("golang", ".go");
        fileExtensions.put("java", ".java");

        // Printing the TreeMap (Output will be sorted based on keys)
        System.out.println(fileExtensions);
        
        System.out.println("First Kay :" + fileExtensions.firstKey());
        
        System.out.println("Last Kay :" + fileExtensions.lastKey());
        
        SortedMap<String, String> sortedMap = fileExtensions.tailMap("java");
        System.out.println("tailMap : " + sortedMap);
        
        sortedMap = fileExtensions.headMap("java");
        System.out.println("headMap : " + sortedMap);
    }
}
Output:
{c++=.cpp, golang=.go, java=.java, kotlin=.kt, python=.py}
First Kay :c++
Last Kay :python
tailMap : {java=.java, kotlin=.kt, python=.py}
headMap : {c++=.cpp, golang=.go}

SortedMap Interface Hierarchy Diagram

SortedMap Interface APIs/Methods

  • Comparator<? super K> 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<Map.Entry<K,V>> entrySet() - Returns a Set view of the mappings contained in this map.
  • K firstKey() - Returns the first (lowest) key currently in this map.
  • SortedMap<K,V> headMap(K 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.
  • K lastKey() - Returns the last (highest) key currently in this map.
  • SortedMap<K,V> subMap(K fromKey, K toKey) - Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.
  • SortedMap<K,V> tailMap(K 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.

SortedMap Interface Implementations

SortedMap interface extends Map interface so it inherits all the methods and properties of it. The SortedMap interface can be implemented using all the Map Interface Implementations.
We will learn SortedMap interface with TreeMap implementation class with examples. There separate post for TreeMap implementation class.

TreeMap Implementation Class

  1. Overview of TreeMap class
  2. Create TreeMap Example
  3. TreeMap Sorting Order Example(Ascending Order)
  4. TreeMap with a custom Comparator (Descending Order)
  5. Accessing the entries of a TreeMap

Learn TreeMap on  Guide to TreeMap Class

Comments