📜  Java中的 NavigableMap highEntry() 方法

📅  最后修改于: 2022-05-13 01:54:54.598000             🧑  作者: Mango

Java中的 NavigableMap highEntry() 方法

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

语法

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

其中,K 是此映射维护的键的类型,V 是映射到键的值的类型。

参数:此函数接受单个参数Key ,该参数引用此地图容器维护的键的类型。

返回值:它返回与严格大于给定键的最小键关联的键值映射,如果不存在这样的键,则返回 null。

下面的程序说明了Java中的 HigherEntry() 方法:

程序1 :当key为整数时。

// Java code to demonstrate the working of
// higherEntry() method
  
import java.io.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the NavigableMap of Integer and String
        NavigableMap nmmp = new TreeMap<>();
  
        // assigning the values in the NavigableMap
        // using put()
        nmmp.put(2, "two");
        nmmp.put(7, "seven");
        nmmp.put(3, "three");
  
        System.out.println("The mapping with least key is : "
                           + nmmp.higherEntry(2));
    }
}
输出:
The mapping with least key is : 3=three

方案二:当key为字符串时。

// Java code to demonstrate the working of
// higherEntry() method
  
import java.io.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the NavigableMap of Integer and String
        NavigableMap tmmp = new TreeMap<>();
  
        // assigning the values in the NavigableMap
        // using put()
        tmmp.put("one", "two");
        tmmp.put("six", "seven");
        tmmp.put("two", "three");
  
        System.out.println("The mapping associated with the least key is : "
                           + tmmp.higherEntry("one"));
    }
}
输出:
The mapping associated with the least key is : six=seven

参考:https: Java/util/NavigableMap.html#higherEntry(K)