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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。