有 二 Java中ceilingEntry()的变体。util。TreeMap,本文将讨论这两种方法。
null
1.ceilingEntry(K键): 它用于返回 键值映射 与 大于或等于给定密钥的最小密钥, 如果没有这样的密钥,则返回null。
Syntax : public Map.Entry ceilingEntry(K key) Parameters : key : The key to be matched. Return Value : It returns the entry with the least key greater than or equal to key, and null if there is no such key. Exception : ClassCastException : It throws the exception if the specified key cannot be compared with the keys currently in the map. NullPointerException : It throws the exception if the specified key is null.
// Java code to demonstrate the working of // ceilingEntry() import java.io.*; import java.util.*; public class ceilingEntry1 { public static void main(String[] args) { // Declaring the tree map of Integer and String TreeMap<Integer, String> treemap = new TreeMap<Integer, String>(); // assigning the values in the tree map // using put() treemap.put( 2 , "two" ); treemap.put( 7 , "seven" ); treemap.put( 3 , "three" ); // Use of ceilingEntry() // returns 7=seven ( next greater key-value) System.out.println( "The next greater key-value of 5 is : " + treemap.ceilingEntry( 5 )); // returns "null" as no value present // greater than or equal to number System.out.println( "The next greater key-value of 8 is : " + treemap.ceilingEntry( 8 )); } } |
输出:
The next greater key-value of 5 is : 7=seven The next greater key-value of 8 is : null
2.天花板键(K键): 这也有同样的工作,作为上面的一个,但唯一的区别是 它不包含映射的键 。它只返回大于或等于给定密钥的最小密钥,否则返回NULL。
Syntax : public K ceilingKey(K key) Parameters : key : The key to be matched. Return Value : It returns the entry with the least key greater than or equal to key, and null if there is no such key. Exception: ClassCastException : It throws the exception if the specified key cannot be compared with the keys currently in the map. NullPointerException : It throws the exception if the specified key is null.
// Java code to demonstrate the working of // ceilingKey() import java.io.*; import java.util.*; public class ceilingKey1 { public static void main(String[] args) { // Declaring the tree map of Integer and String TreeMap<Integer, String> treemap = new TreeMap<Integer, String>(); // assigning the values in the tree map // using put() treemap.put( 2 , "two" ); treemap.put( 7 , "seven" ); treemap.put( 3 , "three" ); // Use of ceilingKey() // returns 7 ( next greater key) System.out.println( "The next greater key of 5 is : " + treemap.ceilingKey( 5 )); // returns "null" as no key present // greater than or equal to number System.out.println( "The next greater key of 8 is : " + treemap.ceilingKey( 8 )); } } |
输出:
The next greater key of 5 is : 7 The next greater key of 8 is : null
本文由 沙姆巴维·辛格 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END