Java中的 ConcurrentSkipListSet tailSet() 方法及示例
Java .util.concurrent.ConcurrentSkipListSet的tailSet()方法是Java中的一个内置函数,其中返回等于或大于指定元素的元素。
该函数的语法给出了对指定元素的清晰理解,后面是示例。
句法:
tailSet(E fromElement)
or
tailSet(E fromElement, boolean inclusive)
参数:
此方法的第一个变体仅采用一个参数,即 fromElement E从集合中返回大于或等于该元素的元素。
第二个变体类似于第一个变体,但这里第二个参数是布尔值,如果它设置为 false
那么元素E (如果存在于列表中)将不包括在内。
回报:
此方法返回此集合中元素大于或等于 fromElement 的部分的视图。
在第二个变体的情况下,这个 fromElement 的包含是由布尔类型决定的。
例外:
空指针异常:如果指定元素为空。
以下是说明Java中的 ConcurrentSkipListSet tailSet() 的示例程序:
示例:1
返回大于 200 的元素。
Java
// Java program to demonstrate ConcurrentSkipListSet tailSet() method
import java.util.concurrent.ConcurrentSkipListSet;
class ConcurrentSkipListSetLastExample1 {
public static void main(String[] args)
{
// Initializing the set using ConcurrentSkipListSet()
ConcurrentSkipListSet
set = new ConcurrentSkipListSet();
// Adding elements to this set
set.add(199);
set.add(256);
set.add(958);
set.add(421);
set.add(80);
// Printing the ConcurrentSkipListSet
System.out.println("ConcurrentSkipListSet: "
+ set);
// Printing the elements of ConcurrentSkipListSet that
// are returned by tailSet() method
System.out.println("The returned elements are: "
+ set.tailSet(200));
}
}
Java
// Java program to demonstrate ConcurrentSkipListSet tailSet() method
import java.util.concurrent.ConcurrentSkipListSet;
class ConcurrentSkipListSetLastExample1 {
public static void main(String[] args)
{
// Initializing the set using ConcurrentSkipListSet()
ConcurrentSkipListSet
set = new ConcurrentSkipListSet();
// Adding elements to this set
set.add(13);
set.add(35);
set.add(9);
set.add(41);
set.add(90);
// Printing the ConcurrentSkipListSet
System.out.println("ConcurrentSkipListSet: "
+ set);
// Printing the elements of ConcurrentSkipListSet that
// are returned by tailSet() method
System.out.println("The returned elements are: "
+ set.tailSet(35, false));
}
}
输出:
ConcurrentSkipListSet: [80, 199, 256, 421, 958]
The returned elements are: [256, 421, 958]
示例:2
在此示例中,不返回元素 35,因为 boolean inclusive 为 false。
Java
// Java program to demonstrate ConcurrentSkipListSet tailSet() method
import java.util.concurrent.ConcurrentSkipListSet;
class ConcurrentSkipListSetLastExample1 {
public static void main(String[] args)
{
// Initializing the set using ConcurrentSkipListSet()
ConcurrentSkipListSet
set = new ConcurrentSkipListSet();
// Adding elements to this set
set.add(13);
set.add(35);
set.add(9);
set.add(41);
set.add(90);
// Printing the ConcurrentSkipListSet
System.out.println("ConcurrentSkipListSet: "
+ set);
// Printing the elements of ConcurrentSkipListSet that
// are returned by tailSet() method
System.out.println("The returned elements are: "
+ set.tailSet(35, false));
}
}
输出:
ConcurrentSkipListSet: [9, 13, 35, 41, 90]
The returned elements are: [41, 90]