在Java中使用示例的ConcurrentSkipListSet tailSet()方法
java.util.concurrent.ConcurrentSkipListSet 的 tailSet () 方法是Java中的一种内置函数,其中返回等于或大于指定元素的元素。
函数的语法清楚地说明了指定元素的示例后跟着的内容。
语法:
tailSet(E fromElement)
or
tailSet(E fromElement, boolean inclusive)
参数:
此方法的第一个变体只采用一个参数,即 fromElement E 从此元素大于或等于该元素的元素从集合中返回。
第二个变体类似于第一个变体,但这里的第二个参数是布尔值“包含”,如果设置为 false
那么该元素 E (如果存在于列表中)将不包括在内。
返回:
此方法返回部分集合的视图,该视图的元素大于或等于 fromElement。
在第二个变体的情况下,包含此 fromElement 的包含由布尔类型决定。
异常:
空指针异常: 如果指定元素为 NULL。
下面是用于说明Java中的ConcurrentSkipListSet tailSet()的示例程序:
示例:1
返回大于200的元素。
// 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<Integer>
set = new ConcurrentSkipListSet<Integer>();
// 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));
}
}
输出:
ConcurrentSkipListSet: [80, 199, 256, 421, 958]
The returned elements are: [256, 421, 958]
示例:2
在此示例中,元素35未返回,因为布尔包含为false。
// 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<Integer>
set = new ConcurrentSkipListSet<Integer>();
// 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通过tailSet()方法返回的元素。
System.out.println("The returned elements are: "
+ set.tailSet(35, false));
}
}
输出结果:
ConcurrentSkipListSet: [9, 13, 35, 41, 90]
返回的元素是:[41, 90]
极客教程