Java中的 ConcurrentSkipListSet subSet() 方法
子集(E fromElement,E toElement)
Java .util.concurrent.ConcurrentSkipListSet的subSet()方法是Java中的一个内置函数,它返回该集合部分的视图,该部分的元素范围从包含 fromElement 到不包含 toElement。 (如果 fromElement 和 toElement 相等,则返回的集合为空。)返回的集合由该集合支持,因此返回集合中的更改会反映在该集合中,反之亦然。返回的集合支持该集合支持的所有可选集合操作。
句法:
public NavigableSet subSet(E fromElement,
E toElement)
参数:该函数接受以下参数:
返回值:该函数返回一个 NavigableSet,它是该集合的一部分的视图,其元素范围从 fromElement(包括)到 toElement(不包括)。
异常:该函数抛出以下异常:
下面的程序说明了 ConcurrentSkipListSet.subSet() 方法:
方案一:
// 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
set = new ConcurrentSkipListSet();
// 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 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]
方案二:
// 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
set = new ConcurrentSkipListSet();
// 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 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
subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
Java .util.concurrent.ConcurrentSkipListSet的subSet()方法是Java中的一个内置函数,它返回该集合中元素范围从 fromElement 到 toElement 的部分的视图。如果 fromElement 和 toElement 相等,则返回的集合为空,除非 fromInclusive 和 toInclusive 都为真。返回的集合由该集合支持,因此返回集合中的更改会反映在该集合中,反之亦然。返回的集合支持该集合支持的所有可选集合操作。返回的集合将在尝试插入超出其范围的元素时抛出 IllegalArgumentException。
句法:
public NavigableSet subSet(E fromElement,
boolean fromInclusive,
E toElement,
boolean toInclusive)
参数:该函数接受以下参数:
返回值:该函数返回该集合的一部分的视图,其元素范围从 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
set = new ConcurrentSkipListSet();
// 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 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 Java
https://docs.oracle.com/javase/8/docs/api/java Java