當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。