Java中的ConcurrentSkipListSet subSet()方法

子集(E fromElement,E toElement)

这个 子集() 方法 JAVAutil。同时发生的ConcurrentSkipListSet 是Java中的一个内置函数,它返回该集合中元素范围从fromElement,inclusive到toElement,exclusive的部分的视图。(如果fromElement和toElement相等,则返回的集为空。)返回的集合由该集合支持,因此返回集合中的更改会反映在该集合中,反之亦然。返回的集合支持该集合支持的所有可选集合操作。

null

语法:

public NavigableSet subSet(E fromElement,
                     E toElement)

参数: 该函数接受以下参数:

  • fromElement –返回集的低端(含低端)。
  • 托伦 –返回集的高端(独占)

    返回值: 该函数返回一个NavigableSet,它是该集合中元素范围从fromElement,inclusive到toElement,exclusive的部分的视图。

    例外情况: 该函数引发以下异常:

  • 类别例外 –如果fromElement和toElement不能使用该集合的比较器相互比较(或者,如果集合没有比较器,则使用自然排序)。如果fromElement或toElement无法与集合中当前的元素进行比较,则实现可能会(但不要求)引发此异常。
  • 空指针异常 –如果fromElement或toElement为空
  • 非法数据异常 –如果fromElement大于toElement;或者,如果这个集合本身有一个限制范围,并且fromElement或toElement位于范围的边界之外。

    下面的程序演示了ConcurrentSkipListSet。subSet()方法:

    项目1:

    // Java program to demonstrate subSet()
    // method of ConcurrentSkipListSet
    // Java Program Demonstrate subSet()
    // method of ConcurrentSkipListSet */
    import java.util.NavigableSet;
    import java.util.concurrent.ConcurrentSkipListSet;
    class ConcurrentSkipListSetSubSetExample1 {
    public static void main(String[] args)
    {
    // Initializing the set
    ConcurrentSkipListSet<Integer>
    set = new ConcurrentSkipListSet<Integer>();
    // Adding elements to this set
    for ( int i = 0 ; i <= 10 ; i++)
    set.add(i);
    // Printing the elements of the set
    System.out.println( "Contents of the set: " + set);
    // Creating a subsetset object
    NavigableSet<Integer> sub_set = set.subSet( 2 , 8 );
    // Printing the elements of the descending set
    System.out.println( "Contents of the subset: " + sub_set);
    }
    }

    
    

    输出:

    Contents of the set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    Contents of the subset: [2, 3, 4, 5, 6, 7]
    

    项目2:

    // Java program to demonstrate subSet()
    // method of ConcurrentSkipListSet
    // Java Program Demonstrate subSet()
    // method of ConcurrentSkipListSet */
    import java.util.NavigableSet;
    import java.util.concurrent.ConcurrentSkipListSet;
    class ConcurrentSkipListSetSubSetExample2 {
    public static void main(String[] args)
    {
    // Initializing the set
    ConcurrentSkipListSet<Integer>
    set = new ConcurrentSkipListSet<Integer>();
    // Adding elements to this set
    for ( int i = 0 ; i <= 10 ; i++)
    set.add(i);
    // Printing the elements of the set
    System.out.println( "Contents of the set: " + set);
    try {
    // Creating a subsetset object
    NavigableSet<Integer> sub_set = set.subSet( 2 , null );
    }
    catch (Exception e) {
    System.out.println( "Exception: " + e);
    }
    }
    }

    
    

    输出:

    Contents of the set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    Exception: java.lang.NullPointerException
    

    子集(E fromElement、布尔fromInclusive、E toElement、布尔toInclusive)

    这个 子集() 方法 JAVAutil。同时发生的ConcurrentSkipListSet 是Java中的一个内置函数,它返回该集合中元素范围从Element到toElement的部分的视图。如果fromElement和toElement相等,则返回的集为空,除非fromInclusive和toInclusive都为真。返回的集合由该集合支持,因此返回集合中的更改会反映在该集合中,反之亦然。返回的集合支持该集合支持的所有可选集合操作。如果试图在其范围外插入元素,返回的集合将抛出IllegalArgumentException。

    语法:

    public NavigableSet subSet(E fromElement,
                         boolean fromInclusive,
                         E toElement,
                         boolean toInclusive)
    

    参数: 该函数接受以下参数:

  • fromElement –返回集的低端
  • 从包含 –如果低端要包含在返回的视图中,则为true
  • 托伦 –返回集的高端
  • 包括 –如果高端要包含在返回的视图中,则为true

    返回值: 该函数返回该集合中元素范围从fromElement,inclusive到toElement,exclusive的部分的视图。

    例外情况: 该函数引发以下异常:

  • 类别例外 –如果fromElement和toElement不能使用该集合的比较器相互比较(或者,如果集合没有比较器,则使用自然排序)。如果fromElement或toElement无法与集合中当前的元素进行比较,则实现可能会(但不要求)引发此异常。
  • 空指针异常 –如果fromElement或toElement为空
  • 非法数据异常 –如果fromElement大于toElement;或者,如果这个集合本身有一个限制范围,并且fromElement或toElement位于范围的边界之外。

    下面的程序演示了ConcurrentSkipListSet。subSet()方法: 方案3:

    // Java Program Demonstrate subSet()
    // method of ConcurrentSkipListSet */
    import java.util.NavigableSet;
    import java.util.concurrent.ConcurrentSkipListSet;
    class ConcurrentSkipListSetSubSetExample3 {
    public static void main(String[] args)
    {
    // Initializing the set
    ConcurrentSkipListSet<Integer>
    set = new ConcurrentSkipListSet<Integer>();
    // Adding elements to this set
    for ( int i = 0 ; i <= 10 ; i++)
    set.add(i);
    // Printing the elements of the set
    System.out.println( "Contents of the set: " + set);
    // Creating a subsetset object
    NavigableSet<Integer> sub_set = set.subSet( 2 , true , 8 , true );
    // Printing the elements of the descending set
    System.out.println( "Contents of the subset: " + sub_set);
    }
    }

    
    

    输出:

    Contents of the set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    Contents of the subset: [2, 3, 4, 5, 6, 7, 8]
    

    参考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#subSet-E-E-

    https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#subSet-E-布尔-E-布尔-

  • © 版权声明
    THE END
    喜欢就支持一下吧
    点赞8 分享