Java中的NavigableMap ceilingKey()方法

的ceilingKey()方法 Java中的NavigableMap接口 用于返回大于或等于给定密钥的最小密钥,如果不存在此类密钥,则返回null。

null

语法 :

K ceilingKey(K key)

其中,K是该映射容器维护的密钥类型。

参数 :它只接受一个参数 钥匙 这是要映射的密钥,属于此集合接受的密钥类型。

返回值 :它返回大于或等于给定密钥的最小密钥,如果不存在此类密钥,则返回null。

下面的程序演示了Java中的ceilingKey()方法:

方案1 :当键为整数时。

// Java code to demonstrate the working of
// ceilingKey()  method
import java.io.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the NavigableMap of Integer and String
NavigableMap<Integer, String> tmmp = new TreeMap<>();
// assigning the values in the NavigableMap
// using put()
tmmp.put( 2 , "two" );
tmmp.put( 7 , "seven" );
tmmp.put( 3 , "three" );
// Use of ceilingKey()
// returns 7( next greater key)
System.out.println( "The next greater key of 6 is : "
+ tmmp.ceilingKey( 6 ));
// returns "null" as no value present
// greater than or equal to number
System.out.println( "The next greater key of 3 is : "
+ tmmp.ceilingKey( 3 ));
}
}


输出:

The next greater key of 6 is : 7
The next greater key of 3 is : 3

方案2 :当键为string时。

// Java code to demonstrate the working of
// ceilingKey()  method
import java.io.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the NavigableMap of Integer and String
NavigableMap<String, String> tmmp = new TreeMap<>();
// assigning the values in the NavigableMap
// using put()
tmmp.put( "one" , "two" );
tmmp.put( "six" , "seven" );
tmmp.put( "two" , "three" );
// Use of ceilingKey()
// returns 7( next greater key)
System.out.println( "The next greater key of five is : "
+ tmmp.ceilingKey( "five" ));
// returns "null" as no value present
// greater than or equal to number
System.out.println( "The next greater key of six is : " +
tmmp.ceilingKey( "six" ));
}
}


输出:

The next greater key of five is : one
The next greater key of six is : six

参考 : https://docs.oracle.com/javase/10/docs/api/java/util/NavigableMap.html#ceilingKey(K)

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享