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


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


TreeSet 是 Java 中 SortedSet interface 最重要的实现之一,它使用 Tree 进行存储。无论是否提供显式的comparator,元素的排序均由集合使用其自然顺序来维护。如果要正确实现 Set interface ,这必须与 equals 一致。

java.util 包中存在的 TreeSet class 的 headSet() 方法用作树集的限制设置器,以排序的方式返回不超过方法参数中定义的限制的元素(不包括元素)。

用法:

head_set = (TreeSet)tree_set.headSet(Object element)

参数:参数元素是树集的类型,并且是头点,它是允许树返回不包括元素本身。

返回值:该方法以严格小于参数中提到的元素的排序方式返回值的部分。

现在我们将讨论在 TreeSet 类中实现 headSet() 方法时的不同场景:

  • 情况1:在排序的TreeSet中
  • 案例2-A:在未排序的TreeSet中
  • 情况2-B:在未排序的 TreeSet 中,但具有 String 类型元素

示例 1:

Java


// Java program to Illustrate headSet() method 
// of TreeSet class In a sorted TreeSet 
  
// Importing required classes 
import java.io.*; 
import java.util.Iterator; 
import java.util.TreeSet; 
  
// Main class 
public class GFG { 
  
    // Main driver method 
    public static void main(String[] args) 
    { 
        // Creating an empty TreeSet by 
        // declaring object of TreeSet class 
        TreeSet<Integer> tree_set = new TreeSet<Integer>(); 
  
        // Adding the elements 
        // using add() method 
        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: "); 
  
        // Holds true till there is single element 
        // remaining in the object 
        while (iterate.hasNext()) { 
  
            // Iterating through the headSet 
            // using next() method 
            System.out.println(iterate.next() + " "); 
        } 
    } 
}
输出:
The resultant values till head set: 
1 
2 
3 
4 
5 
10 
20

实施例2-A:

Java


// Java Program to illustrate headSet() method 
// of TreeSet class In an unsorted TreeSet 
  
// Importing required classes  
import java.io.*; 
import java.util.Iterator; 
import java.util.TreeSet; 
  
// Main class  
public class GFG { 
    
    // Main driver method  
    public static void main(String[] args) 
    { 
  
        // Creating an empty TreeSet 
        TreeSet<Integer> tree_set = new TreeSet<Integer>(); 
  
        // Adding the elements 
        // using add() method  
        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()) { 
            
            // Printing the elements  
            System.out.println(iterate.next() + " "); 
        } 
    } 
} 
输出:
The resultant values till head set: 
2 
9 
10 
15 
16 
20

实施例2-B:

Java


// Java code to illustrate headSet() method of TreeSet class 
// In an unsorted treeset but with String type elements 
  
// Importing required classes 
import java.io.*; 
import java.util.Iterator; 
import java.util.TreeSet; 
  
// Main class 
public class GFG { 
  
    // Main driver method 
    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()) { 
  
            // Printing elements using next() method 
            System.out.println(iterate.next() + " "); 
        } 
    } 
}
输出:
The resultant values till head set: 
4 
Geek 
Geeks


相关用法


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