Java NavigableMap ceilingEntry()方法

Java NavigableMap ceilingEntry()方法

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

语法:

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

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

返回值 :它返回一个与大于或等于给定键的最小键相关的键值映射,如果没有这样的键存在,则返回空。

以下程序说明了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)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程