这个 ceilingKey()函数 属于 树映射类 返回 大于或等于给定密钥的最小密钥,如果缺少此类密钥,则为null。
null
语法:
public K ceilingKey(K key)
参数: 此方法接受一个强制参数 钥匙 这是需要搜索的关键。
返回值: 此方法返回 最不重要的 大于或等于给定的键值。 如果缺少这样的密钥,则返回null。
例外情况: 此方法引发以下异常:
- 类别例外 –如果指定的键无法与给定的键值进行比较,则引发。
- 空指针异常 –如果给定的键为空,并且映射使用自然排序,或者比较器不允许空值,则抛出。
下面是ceilingKey()方法的示例:
项目1: 演示ceilingKey()方法在带比较器的树状图中的使用
import java.util.*; public class Main { public static void main(String[] args) { // creating tree map NavigableMap<Integer, String> treemap = new TreeMap<Integer, String>((a, b) -> ((a > b) ? 1 : ((a == b) ? 0 : - 1 ))); // populating tree map treemap.put( 1 , " A " ); treemap.put( 2 , " B " ); treemap.put( 3 , " C " ); treemap.put( 4 , " D " ); treemap.put( 6 , " E " ); try { System.out.println( "Ceiling key entry for 5: " + treemap.ceilingKey( 5 )); } catch (Exception e) { System.out.println( "Exception: " + e); } } } |
输出:
Ceiling key entry for 5: 6
项目2: 演示ceilingKey()方法在无任何比较器的树状图中的使用
import java.util.*; public class Main { public static void main(String[] args) { // creating tree map NavigableMap<Integer, String> treemap = new TreeMap<Integer, String>(); // populating tree map treemap.put( 1 , " A " ); treemap.put( 2 , " B " ); treemap.put( 3 , " C " ); treemap.put( 4 , " D " ); treemap.put( 6 , " E " ); treemap.put( 7 , " F " ); // Since 6 is the least value greater than 5, // it is returned as the key. System.out.println( "Ceiling key entry for 5: " + treemap.ceilingKey( 5 )); } } |
输出:
Ceiling key entry for 5: 6
方案3: 演示ceilingKey()方法在返回null时的用法
import java.util.*; public class Main { public static void main(String[] args) { // creating tree map NavigableMap<Integer, String> treemap = new TreeMap<Integer, String>(); // populating tree map treemap.put( 1 , " A " ); treemap.put( 2 , " B " ); treemap.put( 3 , " C " ); treemap.put( 4 , " E " ); treemap.put( 5 , " D " ); // Since 10 is not present in the Map // and neither any Key is present greater than 10 // Hence this will return null System.out.println( "Ceiling key entry for 10: " + treemap.ceilingKey( 10 )); } } |
输出:
Ceiling key entry for 10: null
方案4: 显示NullPointerException的步骤
import java.util.*; public class Main { public static void main(String[] args) { // creating tree map TreeMap<Integer, String> treemap = new TreeMap<Integer, String>(); // populating tree map treemap.put( 2 , " two " ); treemap.put( 1 , " one " ); treemap.put( 3 , " three " ); treemap.put( 6 , " six " ); treemap.put( 5 , " five " ); try { // returns a NullPointerException // as key value can't be null // because of natural ordering System.out.println( "Ceiling key entry for null value : " + treemap.ceilingKey( null )); } catch (Exception e) { System.out.println( "Exception: " + e); } } } |
输出:
Exception: java.lang.NullPointerException
方案5: 为了证明ClassCastException
import java.util.*; public class Main { public static void main(String[] args) { // creating tree map NavigableMap<Object, String> treemap = new TreeMap<Object, String>(); // populating tree map treemap.put( 1 , " A " ); treemap.put( 2 , " B " ); treemap.put( 3 , " C " ); treemap.put( 4 , " E " ); treemap.put( 5 , " D " ); try { // returns ClassCastException // as we cannot compare a String object with an Integer object System.out.println( "Ceiling key entry for "asd": " + treemap.ceilingKey( new String( "asd" ))); } catch (Exception e) { System.out.println( "Exception: " + e); } } } |
输出:
Exception: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END