当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java TreeSet headSet()用法及代码示例


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


相关用法


注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 TreeSet headSet() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。