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


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