Saturday, November 22, 2025
HomeLanguagesJavaSortedMap subMap() method in Java

SortedMap subMap() method in Java

The subMap() method of SortedMap interface in Java is used to return a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.

  • The map returned by this method is backed by this map, so changes in the returned map are reflected in this map, and vice-versa.
  • The map returned by this method supports all optional map operations that this map supports.

Note: The map returned by this method will throw an IllegalArgumentException if an attempt is made to insert a key outside its range.

Syntax:

SortedMap<K, V> subMap(K fromKey,
                      K toKey)

Where, K is the type of key maintained by this Set and V is the type of values associated with the Key.

Parameters: This function accepts two parameter fromKey and toKey which represents low endpoint (inclusive) and high endpoint (exclusive) respectively of the keys in the returned map.

Return Value: It returns a view of the portion of this map whose keys range from fromKey to toKey.

Exception:

  • ClassCastException: If the parameter fromKey is not compatible with this map’s comparator (or, if the map has no comparator, if fromKey does not implement Comparable).
  • NullPointerException: If the parameter fromKey is null and this map does not permit null keys.
  • IllegalArgumentException: If this map itself has a restricted range, and fromKey lies outside the bounds of the range

Below programs illustrate the above method:

Program 1:




// A Java program to demonstrate
// working of SortedSet
import java.util.*;
  
public class Main {
    public static void main(String[] args)
    {
        // Create a TreeSet and inserting elements
        SortedMap<Integer, String> mp = new TreeMap<>();
  
        // Adding Element to SortedSet
        mp.put(1, "One");
        mp.put(2, "Two");
        mp.put(3, "Three");
        mp.put(4, "Four");
        mp.put(5, "Five");
  
        // Returning the key ranging
        // between 2 and 5
        System.out.print("Elements in range from 2 to 5 in the map is : "
                         + mp.subMap(2, 5));
    }
}


Output:

Elements in range from 2 to 5 in the map is : {2=Two, 3=Three, 4=Four}

Program 2:




// A Java program to demonstrate
// working of SortedSet
import java.util.*;
  
public class Main {
    public static void main(String[] args)
    {
        // Create a TreeSet and inserting elements
        SortedMap<String, String> mp = new TreeMap<>();
  
        // Adding Element to SortedSet
        mp.put("One", "Geeks");
        mp.put("Two", "For");
        mp.put("Three", "Geeks");
        mp.put("Four", "Code");
        mp.put("Five", "It");
  
        // Returning the key range between D and Z
        System.out.print("Key in range from D to Z in the map is : "
                         + mp.subMap("D", "Z"));
    }
}


Output:

Key in range from D to Z in the map is : {Five=It, Four=Code, One=Geeks, Three=Geeks, Two=For}

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/SortedMap.html#subMap(K)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6785 POSTS0 COMMENTS
Nicole Veronica
11932 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12000 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6864 POSTS0 COMMENTS
Umr Jansen
6852 POSTS0 COMMENTS