Java ConcurrentSkipListSet subSet()方法
subSet(E fromElement, E toElement)
java.util.concurrent.ConcurrentSkipListSet 的 subSet() 方法是Java中的一个内置函数,它返回这个集合中元素范围从fromElement(包括)到toElement(不包括)的部分的视图。(如果fromElement和toElement相等,返回的集合是空的。)返回的集合是由这个集合支持的,所以返回的集合的变化会反映在这个集合中,反之亦然。返回的集合支持这个集合支持的所有可选的集合操作。
语法
public NavigableSet subSet(E fromElement,
E toElement)
参数: 该函数接受以下参数。
- fromElement – 返回集合的低端点(包括)。
- toElement – 返回集合的最高端点(不包括)。
返回值: 该函数返回一个NavigableSet,它是这个集合中元素范围从fromElement(包括)到toElement(包括)的部分的视图。
异常: 该函数会抛出下列异常。
- ClassCastException – 如果fromElement和toElement不能用这个集合的比较器相互比较(或者,如果这个集合没有比较器,则使用自然排序)。如果fromElement或toElement不能与当前集合中的元素进行比较,实现可以抛出这个异常,但不是必须这样做。
- NullPointerException – 如果fromElement或toElement是空的。
- IllegalArgumentException – 如果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
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 – 返回集合的低端点
- fromInclusive – 如果低端点被包含在返回的视图中,则为true。
- toElement – 返回集合的高端点
- toInclusive – 如果返回的视图中包含高端点,则为true。
返回值: 该函数返回该集合中元素范围从fromElement(包括)到toElement(不包括)的部分的视图。
异常: 该函数会抛出以下异常。
- ClassCastException – 如果fromElement和toElement不能用这个集合的比较器相互比较(或者,如果这个集合没有比较器,用自然排序)。如果fromElement或toElement不能与当前集合中的元素进行比较,实现可以抛出这个异常,但不是必须这样做。
- NullPointerException – 如果fromElement或toElement是空的。
- IllegalArgumentException – 如果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-boolean-E-boolean-