📜  Java中的 TreeSet headSet() 方法及示例

📅  最后修改于: 2022-05-13 01:54:20.090000             🧑  作者: Mango

Java中的 TreeSet headSet() 方法及示例

TreeSet 是Java中 SortedSet 接口的最重要实现之一,它使用树进行存储。无论是否提供显式比较器,元素的顺序都由一个集合使用它们的自然顺序来维护。这必须与equals一致才能正确实现Set接口。

TreeSet 类的 headSet() 方法存在于Java.util包中,用作树集的限制设置器,以排除元素的排序方式将元素返回到方法参数中定义的限制。

句法:

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

参数:参数元素是树集的类型,是树的顶点,允许树返回不包括元素本身的值。

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

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

  • 案例 1:在排序的 TreeSet 中
  • 案例 2-A:在未排序的 TreeSet 中
  • 案例 2-B:在未排序的 TreeSet 中但具有字符串类型元素

示例 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 tree_set = new TreeSet();
  
        // 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 head_set = new TreeSet();
  
        // Limiting the values till 5
        head_set = (TreeSet)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() + " ");
        }
    }
}


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 tree_set = new TreeSet();
  
        // 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 head_set = new TreeSet();
  
        // Limiting the values till 5
        head_set = (TreeSet)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() + " ");
        }
    }
}


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 tree_set = new TreeSet();
  
        // 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 head_set = new TreeSet();
  
        // Limiting the values till 5
        head_set = (TreeSet)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: 
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 tree_set = new TreeSet();
  
        // 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 head_set = new TreeSet();
  
        // Limiting the values till 5
        head_set = (TreeSet)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 tree_set = new TreeSet();
  
        // 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 head_set = new TreeSet();
  
        // Limiting the values till 5
        head_set = (TreeSet)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