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


Java ConcurrentSkipListSet higher()用法及代碼示例


java.util.concurrent.ConcurrentSkipListSet的Higher(E e)方法是Java中的內置函數,該函數返回此集合中的最小元素嚴格大於給定元素;如果沒有這樣的元素,則返回null。

用法:

public E higher(E e)

參數:該函數接受單個參數,即要匹配的值


返回值:該函數返回大於e的最小元素;如果沒有這樣的元素,則返回null。

異常:該函數引發以下異常:

  • ClassCastException–如果指定的元素無法與集合中當前的元素進行比較。
  • NullPointerException –如果指定的元素為null

以下示例程序旨在說明ConcurrentSkipListSet.higher(E e)方法:

示例1:

// Java program to demonstrate higher() 
// method of ConcurrentSkipListSet 
  
import java.util.concurrent.*; 
  
class ConcurrentSkipListSetHigherExample1 { 
    public static void main(String[] args) 
    { 
        // Creating a set object 
        ConcurrentSkipListSet<Integer> 
            Lset = new ConcurrentSkipListSet<Integer>(); 
  
        // Adding elements to this set 
        for (int i = 10; i <= 50; i += 10) 
            Lset.add(i); 
  
        // Finding higher of 20 in the set 
        System.out.println("The higher of 20 in the set: "
                           + Lset.higher(20)); 
  
        // Finding higher of 39 in the set 
        System.out.println("The higher of 39 in the set: "
                           + Lset.higher(39)); 
  
        // Finding higher of 50 in the set 
        System.out.println("The higher of 50 in the set: "
                           + Lset.higher(50)); 
    } 
}
輸出:
The higher of 20 in the set: 30
The higher of 39 in the set: 40
The higher of 50 in the set: null

示例2:在higher()中顯示NullPointerException的程序。

// Java program to demonstrate higher() 
// method of ConcurrentSkipListSet 
  
import java.util.concurrent.*; 
  
class ConcurrentSkipListSetHigherExample1 { 
    public static void main(String[] args) 
    { 
        // Creating a set object 
        ConcurrentSkipListSet<Integer> 
            Lset = new ConcurrentSkipListSet<Integer>(); 
  
        // Adding elements to this set 
        for (int i = 10; i <= 50; i += 10) 
            Lset.add(i); 
  
        try { 
            // Trying to find higher of "null" in the set 
            System.out.println("The higher of null in the set: "
                               + Lset.higher(null)); 
        } 
        catch (Exception e) { 
            System.out.println("Exception: " + e); 
        } 
    } 
}
輸出:
Exception: java.lang.NullPointerException

參考:
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#higher-E-



相關用法


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