子集(E fromElement,E toElement)
这个 子集() 方法 JAVAutil。同时发生的ConcurrentSkipListSet 是Java中的一个内置函数,它返回该集合中元素范围从fromElement,inclusive到toElement,exclusive的部分的视图。(如果fromElement和toElement相等,则返回的集为空。)返回的集合由该集合支持,因此返回集合中的更改会反映在该集合中,反之亦然。返回的集合支持该集合支持的所有可选集合操作。
语法:
public NavigableSetsubSet(E fromElement, E toElement)
参数: 该函数接受以下参数:
返回值: 该函数返回一个NavigableSet,它是该集合中元素范围从fromElement,inclusive到toElement,exclusive的部分的视图。
例外情况: 该函数引发以下异常:
下面的程序演示了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 NavigableSetsubSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
参数: 该函数接受以下参数:
返回值: 该函数返回该集合中元素范围从fromElement,inclusive到toElement,exclusive的部分的视图。
例外情况: 该函数引发以下异常:
下面的程序演示了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]