当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java NavigableMap ceilingEntry()用法及代码示例


Java中NavigableMap接口的cielingEntry()方法用于返回与大于或等于给定键的最小键关联的键值映射,如果不存在这样的键,则返回null。

用法

Map.Entry< K, V > ceilingEntry(K key)

参数:它接受单个参数Key,它是要映射的 key 。


返回值:返回与大于或等于给定键的最小键关联的键-值映射关系;如果不存在这样的键,则返回null。

以下程序说明了Java中的ceilingEntry()方法:

程序1:当键为整数时。

// Java code to demonstrate the working of 
// ceilingEntry()  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> navmap = new TreeMap<>(); 
  
        // assigning the values in the NavigableMap 
        // using put() 
        navmap.put(2, "two"); 
        navmap.put(7, "seven"); 
        navmap.put(3, "three"); 
  
        // Use of ceilingEntry() 
        // returns 7=seven ( next greater key-value) 
        System.out.println("The next greater key-value of 5 is : "
                           + navmap.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 : "
                                      + navmap.ceilingEntry(8)); 
    } 
}
输出:
The next greater key-value of 5 is : 7=seven
The next greater key-value of 8 is : null

程序2:当键是字符串时。

// Java code to demonstrate the working of 
// ceilingEntry()  method 
  
import java.io.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the NavigableMap of String and String 
        NavigableMap<String, String> navmap = new TreeMap<String, String>(); 
  
        // assigning the values in the NavigableMap 
        // using put() 
        navmap.put("one", "Geeks"); 
        navmap.put("two", "for"); 
        navmap.put("three", "Geeks"); 
  
        // Use of ceilingEntry() 
        // returns one = Geeks ( next greater key-value of "a") 
        System.out.println("The next greater key-value of a is : "
                           + navmap.ceilingEntry("a")); 
  
        // returns three = Geeks 
        System.out.println("The next greater key-value of p is : "
                                       + navmap.ceilingEntry("p")); 
    } 
}
输出:
The next greater key-value of a is : one=Geeks
The next greater key-value of p is : three=Geeks

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



相关用法


注:本文由纯净天空筛选整理自barykrg大神的英文原创作品 NavigableMap ceilingEntry() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。