• Tags
  •         
  • www.breakyizhan.com
  •    

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

    句法:

    TreeSet tail_set.tailSet(Object element)

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

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

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

     

    程序1:在已排序的树集中。

    // 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<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 tailSet tree
            TreeSet<Integer> tail_set = new TreeSet<Integer>();
    
            // Limiting the values till 5
            tail_set = (TreeSet<Integer>)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<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 tailSet tree
            TreeSet<Integer> tail_set = new TreeSet<Integer>();
    
            // Limiting the values till 5
            tail_set = (TreeSet<Integer>)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中但具有String类型元素。

    // 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<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 tailSet tree
            TreeSet<String> tail_set = new TreeSet<String>();
    
            // Limiting the values till 5
            tail_set = (TreeSet<String>)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

     
    转载请保留页面地址:https://www.breakyizhan.com/java/4925.html