java.util.TreeSet.headSet()方法用作樹集的限製設置器,以排序後的方式將元素返回到該方法的參數中定義的限製(元素除外)。
用法:
head_set = (TreeSet)tree_set.headSet(Object element)
參數:參數element 是樹集的類型,並且是一個上限,該上限是允許樹返回不包含元素本身的值的最大限製。
返回值:該方法以嚴格小於參數中提到的元素的排序方式返回值的一部分。
以下程序說明了java.util.TreeSet.headSet()的用法:
示例1:在排序的樹集中:
// Java code to illustrate headSet()
import java.io.*;
import java.util.Iterator;
import java.util.TreeSet;
public class Tree_Set_Demo {
public static void main(String[] args)
{
// Creating an empty TreeSet
TreeSet<Integer> tree_set = new TreeSet<Integer>();
// Adding the elements using add()
tree_set.add(1);
tree_set.add(2);
tree_set.add(3);
tree_set.add(4);
tree_set.add(5);
tree_set.add(10);
tree_set.add(20);
tree_set.add(30);
tree_set.add(40);
tree_set.add(50);
// Creating the headSet tree
TreeSet<Integer> head_set = new TreeSet<Integer>();
// Limiting the values till 5
head_set = (TreeSet<Integer>)tree_set.headSet(30);
// Creating an Iterator
Iterator iterate;
iterate = head_set.iterator();
// Displaying the tree set data
System.out.println("The resultant values till head set: ");
// Iterating through the headSet
while (iterate.hasNext()) {
System.out.println(iterate.next() + " ");
}
}
}
輸出:
The resultant values till head set: 1 2 3 4 5 10 20
示例2:在未排序的樹集中:
// Java code to illustrate headSet()
import java.io.*;
import java.util.Iterator;
import java.util.TreeSet;
public class Tree_Set_Demo {
public static void main(String[] args)
{
// Creating an empty TreeSet
TreeSet<Integer> tree_set = new TreeSet<Integer>();
// Adding the elements using add()
tree_set.add(9);
tree_set.add(2);
tree_set.add(100);
tree_set.add(40);
tree_set.add(50);
tree_set.add(10);
tree_set.add(20);
tree_set.add(30);
tree_set.add(15);
tree_set.add(16);
// Creating the headSet tree
TreeSet<Integer> head_set = new TreeSet<Integer>();
// Limiting the values till 5
head_set = (TreeSet<Integer>)tree_set.headSet(30);
// Creating an Iterator
Iterator iterate;
iterate = head_set.iterator();
// Displaying the tree set data
System.out.println("The resultant values till head set: ");
// Iterating through the headSet
while (iterate.hasNext()) {
System.out.println(iterate.next() + " ");
}
}
}
輸出:
The resultant values till head set: 2 9 10 15 16 20
示例3:在未排序的樹集中,但具有String類型的元素:
// Java code to illustrate headSet()
import java.io.*;
import java.util.Iterator;
import java.util.TreeSet;
public class Tree_Set_Demo {
public static void main(String[] args)
{
// Creating an empty TreeSet
TreeSet<String> tree_set = new TreeSet<String>();
// Adding the elements using add()
tree_set.add("Welcome");
tree_set.add("To");
tree_set.add("Geek");
tree_set.add("4");
tree_set.add("Geeks");
tree_set.add("TreeSet");
// Creating the headSet tree
TreeSet<String> head_set = new TreeSet<String>();
// Limiting the values till 5
head_set = (TreeSet<String>)tree_set.headSet("To");
// Creating an Iterator
Iterator iterate;
iterate = head_set.iterator();
// Displaying the tree set data
System.out.println("The resultant values till head set: ");
// Iterating through the headSet
while (iterate.hasNext()) {
System.out.println(iterate.next() + " ");
}
}
}
輸出:
The resultant values till head set: 4 Geek Geeks
相關用法
- Java ConcurrentSkipListSet headSet()用法及代碼示例
- Java SortedSet headSet()用法及代碼示例
- Java NavigableSet headSet()用法及代碼示例
- Java TreeSet add()用法及代碼示例
- Java TreeSet contains()用法及代碼示例
- Java TreeSet first()用法及代碼示例
- Java TreeSet last()用法及代碼示例
- Java TreeSet lower()用法及代碼示例
- Java TreeSet isEmpty()用法及代碼示例
- Java TreeSet tailSet()用法及代碼示例
- Java TreeSet clear()用法及代碼示例
- Java TreeSet pollFirst()用法及代碼示例
- Java TreeSet subSet()用法及代碼示例
- Java TreeSet comparator()用法及代碼示例
- Java TreeSet remove()用法及代碼示例
注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 TreeSet headSet() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。