Метод TreeMap tailMap () в Java
Метод java.util.TreeMap.tailMap (from_Key) в Java используется для получения части или представления карты, ключи которой больше, чем равные from_key в параметре. Любые изменения, сделанные на одной карте, будут отражать изменения на другой карте.
Синтаксис:
Tree_Map.tailMap (from_Key)
Параметры: метод принимает один параметр from_key типа Key в TreeMap и ссылается на ключ, который установлен как нижняя точка, более высокая, чем сопоставления, которые должны быть возвращены.
Возвращаемое значение: метод возвращает часть сопоставления, ключи которой больше, чем from_Key.
Исключения: метод выдает три типа исключения:
- ClassCastException: это исключение выдается, если параметры, упомянутые в методе, нельзя сравнить с ключами этой карты.
- NullPointerException : это исключение выдается, если какой-либо из параметров имеет нулевой тип и карта не принимает никаких нулевых значений.
- IllegalArgumentException : это исключение выдается, если упомянутые параметры выходят за пределы допустимого диапазона или нижний предел превышает верхний предел.
Below programs illustrate the working of java.util.TreeMap.tailMap() method:
Program 1:
// 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 )); } } |
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:
// 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" )); } } |
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.
// 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 )); } } |
The original map is: {4=15, Geeks=20, Welcomes=25, You=30} The tailMap is {Welcomes=25, You=30}