當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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