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


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


java.util.concurrent.ConcurrentSkipListSet的subSet()方法是Java中的内置函数,其中元素在此方法定义的范围内返回。该函数的语法使您可以清楚地了解指定元素,并附带示例。

用法:

subSet(E fromElement, E toElement)
           or 
subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)

参数:
此方法的第一个变体采用两个参数,这些参数定义了将要返回的元素的范围。 toElement不包含在返回的集中。


第二个变体与第一个变体相似,但此处的布尔参数专门包含或排除了toElement和fromElement。
返回:
第一个方法变体返回此集合部分的视图,其元素范围从fromElement(包括)到toElement(不包括)。
第二种方法变体返回此集合部分的视图,该元素的范围从boolean inclusive参数定义的fromElement到toElements。

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

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

范例:1

返回从30000到90000的元素,但排除90000。

// Java program to demonstrate ConcurrentSkipListSet subSet() method 
  
import java.util.concurrent.ConcurrentSkipListSet; 
  
class ConcurrentSkipListSetExample { 
    public static void main(String[] args) 
    { 
  
        // Initializing the set using ConcurrentSkipListSet() 
        ConcurrentSkipListSet<Integer> 
            set = new ConcurrentSkipListSet<Integer>(); 
  
        // Adding elements to this set 
        set.add(65552); 
        set.add(34324); 
        set.add(93423); 
        set.add(41523); 
        set.add(90000); 
  
        // Printing the ConcurrentSkipListSet 
        System.out.println("ConcurrentSkipListSet:"
                           + set); 
  
        // Printing the elements of ConcurrentSkipListSet that 
        // are retured by subSet() method 
        System.out.println("The returned elements are:"
                           + set.subSet(30000, 90000)); 
    } 
}
输出:
ConcurrentSkipListSet:[34324, 41523, 65552, 90000, 93423]
The returned elements are:[34324, 41523, 65552]

示例:2
在此示例中,还返回了元素400,因为布尔值包含为true。

// Java program to demonstrate ConcurrentSkipListSet subSet() method 
  
import java.util.concurrent.ConcurrentSkipListSet; 
  
class ConcurrentSkipListSetExample { 
    public static void main(String[] args) 
    { 
  
        // Initializing the set using ConcurrentSkipListSet() 
        ConcurrentSkipListSet<Integer> 
            set = new ConcurrentSkipListSet<Integer>(); 
  
        // Adding elements to this set 
        set.add(652); 
        set.add(324); 
        set.add(400); 
        set.add(123); 
        set.add(200); 
  
        // Printing the ConcurrentSkipListSet 
        System.out.println("ConcurrentSkipListSet:"
                           + set); 
  
        // Printing the elements of ConcurrentSkipListSet that 
        // are retured by subSet() method 
        System.out.println("The returned elements are:"
                           + set.subSet(100, true, 400, true)); 
    } 
}
输出:
ConcurrentSkipListSet:[123, 200, 324, 400, 652]
The returned elements are:[123, 200, 324, 400]


相关用法


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