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


Java ConcurrentSkipListSet tailSet()用法及代码示例


java.util.concurrent.ConcurrentSkipListSet的tailSet()方法是Java中的一个内置函数,其中返回等于或大于指定元素的元素。
该函数的语法使您可以清楚地了解指定元素,并附带示例。

用法:

tailSet(E fromElement)
           or 
tailSet(E fromElement, boolean inclusive)

参数:
此方法的第一个变体仅采用一个参数,即fromElement E,从该参数中返回大于或等于该元素的元素。


第二个变体与第一个变体相似,但如果将第二个参数设置为false,则第二个变体为布尔值
则元素E(如果存在于列表中)将不包括在内。

返回值:
此方法返回此集合中元素大于或等于fromElement的部分的视图。
在第二种情况下,此fromElement的包含由布尔类型决定。

异常:
NullPointerException :如果指定的元素为NULL。

下面是示例程序,用于说明Java中的ConcurrentSkipListSet tailSet():

范例:1

返回大于200的元素。

// Java program to demonstrate ConcurrentSkipListSet tailSet() method 
  
import java.util.concurrent.ConcurrentSkipListSet; 
  
class ConcurrentSkipListSetLastExample1 { 
    public static void main(String[] args) 
    { 
  
        // Initializing the set using ConcurrentSkipListSet() 
        ConcurrentSkipListSet<Integer> 
            set = new ConcurrentSkipListSet<Integer>(); 
  
        // Adding elements to this set 
        set.add(199); 
        set.add(256); 
        set.add(958); 
        set.add(421); 
        set.add(80); 
  
        // Printing the ConcurrentSkipListSet 
        System.out.println("ConcurrentSkipListSet: "
                           + set); 
  
        // Printing the elements of ConcurrentSkipListSet that 
        // are retured by tailSet() method 
        System.out.println("The returned elements are: "
                           + set.tailSet(200)); 
    } 
}
输出:
ConcurrentSkipListSet: [80, 199, 256, 421, 958]
The returned elements are: [256, 421, 958]

示例:2
在该示例中,不返回元素35,因为布尔值包含为false。

// Java program to demonstrate ConcurrentSkipListSet tailSet() method 
  
import java.util.concurrent.ConcurrentSkipListSet; 
  
class ConcurrentSkipListSetLastExample1 { 
    public static void main(String[] args) 
    { 
  
        // Initializing the set using ConcurrentSkipListSet() 
        ConcurrentSkipListSet<Integer> 
            set = new ConcurrentSkipListSet<Integer>(); 
  
        // Adding elements to this set 
        set.add(13); 
        set.add(35); 
        set.add(9); 
        set.add(41); 
        set.add(90); 
  
        // Printing the ConcurrentSkipListSet 
        System.out.println("ConcurrentSkipListSet: "
                           + set); 
  
        // Printing the elements of ConcurrentSkipListSet that 
        // are retured by tailSet() method 
        System.out.println("The returned elements are: "
                           + set.tailSet(35, false)); 
    } 
}
输出:
ConcurrentSkipListSet: [9, 13, 35, 41, 90]
The returned elements are: [41, 90]


相关用法


注:本文由纯净天空筛选整理自ProgrammerAnvesh大神的英文原创作品 ConcurrentSkipListSet tailSet() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。