📜  Java中的 TreeSet tailSet() 方法

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

Java中的 TreeSet tailSet() 方法

Java.util.TreeSet.tailSet() 方法用于设置树集的起点,以排序方式返回所有大于作为参数传递给该方法的参数的元素,包括元素(如果元素在树中提到)。

句法:

TreeSet tail_set.tailSet(Object element)

参数:参数元素是 TreeSet 的类型,是允许树返回大于包含元素的参数中提到的值的值的起点。

返回值:该方法以排序方式返回大于参数中提到的元素(包括参数)的部分值。

下面的程序说明了Java.util.TreeSet.tailSet() 的使用:

程序 1:在排序的 TreeSet 中。

// Java code to illustrate TreeSet.tailSet() method
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 tree_set = new TreeSet();
  
        // 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 tailSet tree
        TreeSet tail_set = new TreeSet();
  
        // Limiting the values till 5
        tail_set = (TreeSet)tree_set.tailSet(10);
  
        // Creating an Iterator
        Iterator iterate;
        iterate = tail_set.iterator();
  
        // Displaying the tree set data
        System.out.println("The resultant values from the tail: ");
  
        // Iterating through the tailSet
        while (iterate.hasNext()) {
            System.out.println(iterate.next() + " ");
        }
    }
}
输出:
The resultant values from the tail: 
10 
20 
30 
40 
50

程序 2:在未排序的 TreeSet 中。

// Java code to illustrate tailSet()
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 tree_set = new TreeSet();
  
        // 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 tailSet tree
        TreeSet tail_set = new TreeSet();
  
        // Limiting the values till 5
        tail_set = (TreeSet)tree_set.tailSet(25);
  
        // Creating an Iterator
        Iterator iterate;
        iterate = tail_set.iterator();
  
        // Displaying the tree set data
        System.out.println("The resultant values from the tail: ");
  
        // Iterating through the tailSet
        while (iterate.hasNext()) {
            System.out.println(iterate.next() + " ");
        }
    }
}
输出:
The resultant values from the tail: 
30 
40 
50 
100

程序 3:在未排序的 TreeSet 中,但具有字符串类型的元素。

// Java code to illustrate tailSet()
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 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 tailSet tree
        TreeSet tail_set = new TreeSet();
  
        // Limiting the values till 5
        tail_set = (TreeSet)tree_set.tailSet("To");
  
        // Creating an Iterator
        Iterator iterate;
        iterate = tail_set.iterator();
  
        // Displaying the tree set data
        System.out.println("The resultant values from the tail: ");
  
        // Iterating through the tailSet
        while (iterate.hasNext()) {
            System.out.println(iterate.next() + " ");
        }
    }
}
输出:
The resultant values from the tail: 
To 
TreeSet 
Welcome