TreeMap headMap() Method in Java

The java.util.TreeMap.headMap(key_point) method of TreeMap class is used to get all the pairs or portion of the map strictly less than the parameter key_value. The mentioned parameter is excluded from the newly prepared treemap. Since the set is backed by the map, so any changes to the map are reflected in the other map, and vice-versa.

Syntax:

sorted_map = old_treemap.headMap(key_point)

Parameters: The method takes one parameter key_point of the type of key taken in the TreeMap and refers to the point, till which the key-value pair is to be returned.

Return Value: The method returns the portion of the treemap whose keys are strictly less than that of the key_point.

Exceptions: The method throws three type of exceptions:

  • ClassCastException: This exception is thrown when the key_point is not compatible or comparable to the maps comparator.
  • NullPointerException: This exception is thrown when key-point is Null.
  • IllegalArgumentException: This exception is thrown when the key_point is out of bound or outside the limit of the map range.

Below programs illustrate the use of java.util.TreeMap.headMap() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate the get() method
import java.util.*;
  
public class Tree_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty TreeMap
        TreeMap<Integer, String> tree_map = new 
                     TreeMap<Integer, String>();
  
        // Mapping string values to int keys
        tree_map.put(10, "Geeks");
        tree_map.put(15, "4");
        tree_map.put(20, "Geeks");
        tree_map.put(25, "Welcomes");
        tree_map.put(30, "You");
  
        // Displaying the TreeMap
        System.out.println("Initial Tree is: "
                                       tree_map);
  
        // Creating the sorted map for map head
        SortedMap<Integer, String> map_head = new 
                       TreeMap<Integer, String>();
        map_head = tree_map.headMap(20);
  
        // Getting the map head
        System.out.println("The headmap is: " + map_head);
    }
}

chevron_right


Output:

Initial Tree is: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
The headmap is: {10=Geeks, 15=4}

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate the get() method
import java.util.*;
  
public class Tree_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty TreeMap
        TreeMap<String, Integer> tree_map = new 
                      TreeMap<String, Integer>();
  
        // Mapping int values to string keys
        tree_map.put("Geeks", 10);
        tree_map.put("4", 15);
        tree_map.put("Geeks", 20);
        tree_map.put("Welcomes", 25);
        tree_map.put("You", 30);
  
        // Displaying the TreeMap
        System.out.println("Initial Tree is: "
                                       tree_map);
  
        // Creating the sorted map for map head
        SortedMap<String, Integer> map_head = new 
                       TreeMap<String, Integer>();
        map_head = tree_map.headMap("You");
  
        // Getting the map head
        System.out.println("The headmap is: " +
                                      map_head);
    }
}

chevron_right


Output:

Initial Tree is: {4=15, Geeks=20, Welcomes=25, You=30}
The headmap is: {4=15, Geeks=20, Welcomes=25}

Note: The same operation can be performed with any type of Mappings with variation and combination of different data types.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.