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


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


ConcurrentSkipListSet的lower()方法用於返回此集合中存在的最大元素,該元素嚴格小於指定元素。如果集合中不存在此類元素,則此函數將返回null。

用法:

public E lower (E e)

參數:此方法僅采用一個參數,e是要匹配的指定值。


返回值:此方法返回的值是集合中存在的最大元素,該值嚴格小於指定元素。如果不存在此類元素,則它將返回Null。

異常:此方法引發以下異常:

  • ClassCastException:如果此集合的元素的類與指定的集合不兼容,則拋出此錯誤。
  • NullPointerException :如果指定的集合為null,則拋出此錯誤。

以下示例程序旨在說明ConcurrentSkipListSet類的lower()函數:

示例1:在下麵的程序中,指定的元素為67,我們的集合也包含元素67,但是由於lower()方法返回的值嚴格較少,因此未返回該元素。

// Java program to demonstrate 
// ConcurrentSkipListSet lower() method. 
  
import java.util.concurrent.ConcurrentSkipListSet; 
  
class GfgCSLS { 
    public static void main(String[] args) 
    { 
  
        // Initializing the set 
        // using ConcurrentSkipListSet() 
        ConcurrentSkipListSet<Integer> 
            set = new ConcurrentSkipListSet<Integer>(); 
  
        // Adding elements to this set 
        // using add() method 
        set.add(17); 
        set.add(24); 
        set.add(35); 
        set.add(67); 
        set.add(98); 
  
        // Printing the ConcurrentSkipListSet 
        System.out.println("ConcurrentSkipListSet: "
                           + set); 
  
        // Invoking lower() method 
        // to find the largest element 
        // less than the specified element 
        System.out.println("Largest element: "
                           + set.lower(67)); 
    } 
}

輸出:

ConcurrentSkipListSet: [17, 24, 35, 67, 98]
Largest element: 35

示例2:顯示NullpointerException

// Java program to demonstrate 
// ConcurrentSkipListSet lower() method. 
  
import java.util.concurrent.ConcurrentSkipListSet; 
  
class GfgCSLS { 
    public static void main(String[] args) 
    { 
  
        // Initializing the set 
        // using ConcurrentSkipListSet() 
        ConcurrentSkipListSet<Integer> 
            set = new ConcurrentSkipListSet<Integer>(); 
  
        // Adding elements to this set 
        // using add() method 
        set.add(17); 
        set.add(24); 
        set.add(35); 
        set.add(67); 
        set.add(98); 
  
        // Printing the ConcurrentSkipListSet 
        System.out.println("ConcurrentSkipListSet: "
                           + set); 
  
        try { 
            // Invoking lower() method 
            // to find the largest element 
            // less than the specified element 
            System.out.println("Largest element: "
                               + set.lower(null)); 
        } 
        catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
}

輸出:

ConcurrentSkipListSet: [17, 24, 35, 67, 98]
java.lang.NullPointerException


相關用法


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