TreeMap subMap() Method in Java
The java.util.TreeMap.subMap(K startKey, K endKey) method in Java is used to return the part or portion of the map defined by the specified range of keys in the parameter. Any changes made in one or the other map will reflect the change in the other map.
Syntax:
Tree_Map.subMap(K startKey, K endKey)
Parameters: The method takes two parameters of Key type:
- startKey: This refers to the starting point or lower end of the map including which the points are to be considered.
- endKey: This refers to the endpoint or the higher end of the map excluding which the points are to be considered.
Note: If startKey is equal to the endKey then a Null Map is returned.
Return Value: The method returns another map containing the part or portion of the map within the specified range.
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.subMap() method:
Program 1:
// Java code to illustrate the subMap() 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 subMap is " + tree_map.subMap(15, 30)); } } |
The original map is: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
The subMap is {15=4, 20=Geeks, 25=Welcomes}
Program 2:
// Java code to illustrate the subMap() 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 subMap System.out.println("The subMap is " + tree_map.subMap("Geeks", "Geeks")); } } |
The original map is: {4=15, Geeks=20, Welcomes=25, You=30}
The subMap is {}
Recommended Posts:
- SortedMap subMap() method in Java
- TreeMap put() Method in Java
- TreeMap get() Method in Java
- TreeMap putAll() Method in Java
- TreeMap remove() Method in Java
- TreeMap values() Method in Java
- TreeMap keySet() Method in Java
- TreeMap lastKey() Method in Java
- TreeMap containsValue() Method in Java
- TreeMap size() Method in Java
- TreeMap clone() Method in Java
- TreeMap containsKey() Method in Java
- TreeMap clear() Method in Java
- TreeMap entrySet() Method in Java
- TreeMap headMap() Method in Java
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.
Improved By : vaibhav2992



