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


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


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

用法:

ConcurrentSkipListSet.floor(E e)

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


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

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

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

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

示例1:

// Java program to demonstrate floor() 
// method of ConcurrentSkipListSet 
  
import java.util.concurrent.*; 
  
class ConcurrentSkipListSetFloorExample1 { 
    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 floor of 20 in the set 
        System.out.println("The floor of 20 in the set "
                           + Lset.floor(20)); 
  
        // Finding floor of 39 in the set 
        System.out.println("The floor of 39 in the set "
                           + Lset.floor(39)); 
  
        // Finding floor of 9 in the set 
        System.out.println("The floor of 10 in the set "
                           + Lset.floor(9)); 
    } 
}
輸出:
The floor of 20 in the set 20
The floor of 39 in the set 30
The floor of 10 in the set null

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

// Java program to demonstrate floor() 
// method of ConcurrentSkipListSet 
  
import java.util.concurrent.*; 
  
class ConcurrentSkipListSetFloorExample2 { 
    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); 
  
        // Trying to find the floor of null 
        try { 
            System.out.println("The floor of null in the set "
                               + Lset.floor(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#floor-E-



相關用法


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