TreeMap tailMap() Method in Java

The java.util.TreeMap.tailMap(from_Key) method in Java is used to get a part or view of the map whose keys are greater than equal to the from_key in the parameter. Any changes made in one map will reflect the change in the other map.

Syntax:

Tree_Map.tailMap(from_Key)

Parameters: The method takes one parameter from_key of the Key type in the TreeMap and refers to the key which is set as the lower point greater than whose mappings are to be returned.



Return Value: The method returns the portion of the mapping whose keys are greater than the from_Key.

Exceptions: The method throws three types of exception:

  • ClassCastException: This exception is thrown if the parameters mentioned in the method cannot be compared with the keys of this map.
  • NullPointerException: This exception is thrown if either of the parameters is of null type and the map does not accept any null values.
  • IllegalArgumentException: This exception is thrown if the mentioned parameters are out of range or the lower end is greater than the higher end.

Below programs illustrate the working of java.util.TreeMap.tailMap() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate the tailMap() 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("The original map is: "
                           + tree_map);
  
        // Displaying the submap
        System.out.println("The tailMap is " + tree_map.tailMap(15));
    }
}

chevron_right


Output:

The original map is: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
The tailMap is {15=4, 20=Geeks, 25=Welcomes, 30=You}

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate the tailMap() 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("The original map is: "
                           + tree_map);
  
        // Displaying the tailMap
        System.out.println("The tailMap is " + tree_map.tailMap("Geeks"));
    }
}

chevron_right


Output:

The original map is: {4=15, Geeks=20, Welcomes=25, You=30}
The tailMap is {Geeks=20, Welcomes=25, You=30}

Program 3: By default, the from_key is included in the tail_Map. If it needs to be ignored or excluded then another parameter can be passed along with the from_key and that is false.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate the tailMap() 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("The original map is: "
                           + tree_map);
  
        // Displaying the tailMap
        System.out.println("The tailMap is " + tree_map.tailMap("Geeks", false));
    }
}

chevron_right


Output:

The original map is: {4=15, Geeks=20, Welcomes=25, You=30}
The tailMap is {Welcomes=25, You=30}


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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.