headSet(E toElement)
java.util.concurrent.ConcurrentSkipListSet.headSet()方法是Java中的内置函数,它返回此集合部分的视图,这些元素的元素严格小于toElement。返回的集合受此集合支持,因此返回的集合中的更改将反映在此集合中,反之亦然。返回的集合支持该集合支持的所有可选集合操作。
用法:
public NavigableSetheadSet(E toElement)
参数:该函数接受元素的单个参数,即返回集的高端点。
返回值:该函数返回一个NavigableSet,该视图是此集合中元素严格小于toElement的部分的视图。
以下示例程序旨在说明ConcurrentSkipListSet.headSet(E toElement)方法:
示例1:
// Java Program Demonstrate headSet()
// method of ConcurrentSkipListSet */
import java.util.NavigableSet;
import java.util.concurrent.ConcurrentSkipListSet;
class ConcurrentSkipListSetHeadSetExample1 {
public static void main(String[] args)
{
// Initializing the set
ConcurrentSkipListSet<Integer>
set = new ConcurrentSkipListSet<Integer>();
// Adding elements to this set
for (int i = 10; i <= 50; i += 10)
set.add(i);
// Creating a headSet object with upper limit 30
NavigableSet<Integer> hd_set = set.headSet(30);
// Printing the elements of the set
System.out.println("Contents of the set: " + set);
// Printing the elements of the headSet set
System.out.println("Contents of the headset"
+ " with upper limit 30: "
+ hd_set);
}
}
Contents of the set: [10, 20, 30, 40, 50] Contents of the headset with upper limit 30: [10, 20]
示例2:显示NullPointerException的程序。
// Java Program Demonstrate headSet()
// method of ConcurrentSkipListSet */
import java.util.NavigableSet;
import java.util.concurrent.ConcurrentSkipListSet;
class ConcurrentSkipListSetHeadSetExample2 {
public static void main(String[] args)
{
// Initializing the set
ConcurrentSkipListSet<Integer>
set = new ConcurrentSkipListSet<Integer>();
// Adding elements to this set
for (int i = 10; i <= 50; i += 10)
set.add(i);
// Printing the elements of the set
System.out.println("Contents of the set: " + set);
try {
// Trying to creating a headSet object with upper limit null
NavigableSet<Integer> hd_set = set.headSet(null);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
// Printing the elements of the headSet set
// System.out.println("Contents of the headset"
+" with upper limit 30: "+ hd_set);
}
}
Contents of the set: [10, 20, 30, 40, 50] Exception: java.lang.NullPointerException
headSet(E toElement, boolean inclusive)
java.util.concurrent.ConcurrentSkipListSet.headSet()方法是Java中的内置函数,该函数返回该集合中元素小于(或等于,如果包含,则为true)toElement的部分的视图。返回的集合受此集合支持,因此返回的集合中的更改将反映在此集合中,反之亦然。返回的集合支持该集合支持的所有可选集合操作。
用法:
public NavigableSetheadSet(E toElement, boolean inclusive)
参数:该函数接受以下参数
- toElement即返回集的高端点。
- inclusive–如果高端端点将包含在返回的视图中,则为true。
返回值:该函数返回一个NavigableSet,该视图是此集合中元素小于(或等于,如果包含在内,则为true)toElement的部分的视图。
以下示例程序旨在说明ConcurrentSkipListSet.headSet(E toElement,包含布尔值)方法:
示例3:
// Java Program Demonstrate headSet()
// method of ConcurrentSkipListSet */
import java.util.NavigableSet;
import java.util.concurrent.ConcurrentSkipListSet;
class ConcurrentSkipListSetHeadSetExample3 {
public static void main(String[] args)
{
// Initializing the set
ConcurrentSkipListSet<Integer>
set = new ConcurrentSkipListSet<Integer>();
// Adding elements to this set
for (int i = 10; i <= 50; i += 10)
set.add(i);
// Creating a headSet object with upper limit 30 inclusive
NavigableSet<Integer> hd_set = set.headSet(30, true);
// Printing the elements of the set
System.out.println("Contents of the set: " + set);
// Printing the elements of the headSet set
System.out.println("Contents of the headset"
+ " with upper limit 30 inclusive: "
+ hd_set);
}
}
Contents of the set: [10, 20, 30, 40, 50] Contents of the headset with upper limit 30 inclusive: [10, 20, 30]
参考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#headSet-E-
相关用法
- Java NavigableSet headSet()用法及代码示例
- Java TreeSet headSet()用法及代码示例
- Java SortedSet headSet()用法及代码示例
- Java ConcurrentSkipListSet last()用法及代码示例
- Java ConcurrentSkipListSet contains()用法及代码示例
- Java ConcurrentSkipListSet add()用法及代码示例
- Java ConcurrentSkipListSet first()用法及代码示例
- Java ConcurrentSkipListSet remove()用法及代码示例
- Java ConcurrentSkipListSet descendingSet()用法及代码示例
- Java ConcurrentSkipListSet size()用法及代码示例
- Java ConcurrentSkipListSet clear()用法及代码示例
- Java ConcurrentSkipListSet clone()用法及代码示例
- Java ConcurrentSkipListSet iterator()用法及代码示例
- Java ConcurrentSkipListSet ceiling()用法及代码示例
- Java ConcurrentSkipListSet isEmpty()用法及代码示例
注:本文由纯净天空筛选整理自rupesh_rao大神的英文原创作品 ConcurrentSkipListSet headSet() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。