java.util.TreeMap类的higherEntry()方法用于返回与严格大于给定键的最小键关联的键-值映射,如果没有这样的键,则返回null。
用法:
public Map.Entry higherEntry(K key)
参数:此方法将 key 作为参数。
返回值:此方法返回一个最小键大于key的条目;如果没有这样的键,则返回null。
异常:如果指定键为null且此映射使用自然顺序,或者其比较器不允许使用null键,则此方法将引发NullPointerException。
以下示例说明了higherEntry()方法
示例1:
// Java program to demonstrate
// higherEntry() method
// for <Integer, String> value
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
// creating object of TreeMap<Integer, String>
TreeMap<Integer, String>
treemap = new TreeMap<Integer, String>();
// populating tree map
treemap.put(1, "One");
treemap.put(2, "Two");
treemap.put(3, "Three");
treemap.put(4, "Four");
treemap.put(5, "Five");
// pritnig the TreeMap
System.out.println("TreeMap: " + treemap);
// getting higher entry value fro 3
Map.Entry<Integer, String>
value
= treemap.higherEntry(3);
// printing the value
System.out.println("The higherEntry value "
+ " for 3: " + value);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出:
TreeMap: {1=One, 2=Two, 3=Three, 4=Four, 5=Five} The higherEntry value for 3: 4=Four
示例2:对于NullPointerException
// Java program to demonstrate
// higherEntry() method
// for NullPointerException
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
// creating object of TreeMap<Integer, String>
TreeMap<Integer, String>
treemap = new TreeMap<Integer, String>();
// populating tree map
treemap.put(1, "One");
treemap.put(2, "Two");
treemap.put(3, "Three");
treemap.put(4, "Four");
treemap.put(5, "Five");
// pritnig the TreeMap
System.out.println("TreeMap: " + treemap);
// getting higher entry value fro null
System.out.println("Trying to get "
+ "the higher entry value"
+ " for null");
Map.Entry<Integer, String>
value
= treemap.higherEntry(null);
// printing the value
System.out.println("Value is: " + value);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出:
TreeMap: {1=One, 2=Two, 3=Three, 4=Four, 5=Five} Trying to get the higher entry value for null Exception thrown : java.lang.NullPointerException
相关用法
- Java NavigableMap higherEntry()用法及代码示例
- Java TreeMap comparator()用法及代码示例
- Java TreeMap navigableKeySet()用法及代码示例
- Java TreeMap lowerEntry()用法及代码示例
- Java TreeMap higherKey()用法及代码示例
- Java TreeMap lowerKey()用法及代码示例
- Java TreeMap ceilingKey()用法及代码示例
- Java TreeMap floorKey()用法及代码示例
- Java TreeMap put()用法及代码示例
- Java TreeMap get()用法及代码示例
- Java TreeMap remove()用法及代码示例
- Java TreeMap putAll()用法及代码示例
- Java TreeMap values()用法及代码示例
- Java TreeMap subMap()用法及代码示例
- Java TreeMap headMap()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 TreeMap higherEntry() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。