爪哇。util。树地图。头像图( 关键点 )TreeMap类的方法用于获得严格小于参数key_值的所有对或部分映射。上述参数不包括在新编制的树状图中。由于集合由映射支持,因此对映射的任何更改都会反映在另一个映射中,反之亦然。
null
语法:
sorted_map = old_treemap.headMap(key_point)
参数: 该方法采用一个参数 关键点 在树映射中获取的键的类型,并指返回键值对的点。
返回值: 该方法返回树映射中键数严格小于关键点的部分。
例外情况: 该方法引发三种类型的异常:
- ClassCastException: 当关键点与maps comparator不兼容或不可比时,会引发此异常。
- NullPointerException: 当key point为Null时引发此异常。
- IllegalArgumentException: 当关键点超出边界或超出贴图范围限制时,会引发此异常。
下面的程序演示了java的使用。util。树地图。headMap()方法: 项目1:
// 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); } } |
输出:
Initial Tree is: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You} The headmap is: {10=Geeks, 15=4}
项目2:
// 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); } } |
输出:
Initial Tree is: {4=15, Geeks=20, Welcomes=25, You=30} The headmap is: {4=15, Geeks=20, Welcomes=25}
注: 相同的操作可以在不同数据类型的变化和组合的任何类型的映射中执行。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END