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


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


java.util.concurrent.ConcurrentSkipListSet.ceiling()方法是Java中的內置函數,該函數返回該集合中大於或等於給定元素的最小元素;如果沒有此類元素,則返回null。

用法:

 ConcurrentSkipListSet.ceiling(E e)

參數:該函數接受單個參數,即要匹配的元素。


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

異常:該函數顯示以下異常:

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

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

示例1:查找數字的上限。

// Java Program Demonstrate ceiling() 
// method of ConcurrentSkipListSet  
  
import java.util.concurrent.*; 
  
class ConcurrentSkipListSetCeilingExample1 { 
    public static void main(String[] args) 
    { 
        // Creating a set object 
        ConcurrentSkipListSet<Integer> Lset =  
                        new ConcurrentSkipListSet<Integer>(); 
  
        // Adding elements to this set 
        Lset.add(45); 
        Lset.add(72); 
        Lset.add(31); 
        Lset.add(13); 
        Lset.add(89); 
  
        // Printing elements of the set 
        System.out.println("The set contains: "); 
        for (Integer i : Lset) 
            System.out.print(i + " "); 
  
        // Ceiling of 35 
        System.out.println("\nCeiling of 35: " + Lset.ceiling(35)); 
  
        // Ceiling of 100 
        System.out.println("\nCeiling of 100: " + Lset.ceiling(100)); 
    } 
}
輸出:
The set contains: 
13 31 45 72 89 
Ceiling of 35: 45

Ceiling of 100: null

示例2:在ceiling()中顯示NullPointerException。

// Java Program Demonstrate ceiling() 
// method of ConcurrentSkipListSet 
  
import java.util.concurrent.*; 
import java.io.*; 
  
class ConcurrentSkipListSetCeilingExample2 { 
    public static void main(String[] args) throws IOException 
    { 
        // Creating a set object 
        ConcurrentSkipListSet<Integer> Lset =  
                            new ConcurrentSkipListSet<Integer>(); 
  
        // Adding elements to this set 
        Lset.add(45); 
        Lset.add(72); 
        Lset.add(31); 
        Lset.add(13); 
        Lset.add(89); 
  
        try { 
            // Ceiling of null 
            System.out.println("Ceiling of null: " + Lset.ceiling(null)); 
        } 
  
        catch (Exception e) { 
            System.out.println("Exception : " + e); 
        } 
    } 
}
輸出:
Exception : java.lang.NullPointerException

參考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#ceiling(E)



相關用法


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