当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。