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


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


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

用法:

TreeSet tail_set.tailSet(Object element)

参数:参数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<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


相关用法


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