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


Java TreeMap higherEntry()用法及代碼示例

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


相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 TreeMap higherEntry() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。