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


Java TreeSet subSet()用法及代碼示例


java.util.TreeSet.subSet()用於返回參數中提到的範圍內的現有TreeSet的子集。該方法接受一個上限和一個下限,並返回範圍內提到的所有元素。如果元素存在於集合中,則包括下限,而排除上限。本質上,它使子集大於等於下限,並且嚴格小於上元素。

用法:

TreeSet tree_set.subSet(Object low_element, Object up_element)

參數:


  • low_element:這是TreeSet的類型,它定義了評估子集的下限或起始元素。該元素包含在子集中。
  • up_element:這是TreeSet的類型,它定義子集要評估的上限或最後一個元素。該元素從子集中排除。

返回值:該方法返回在給定參數範圍內提到的TreeSet類型的子集。

以下程序說明了java.util.TreeSet.subSet()方法:

程序1

// Java code to illustrate subSet() 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(5); 
        tree_set.add(1); 
        tree_set.add(50); 
        tree_set.add(10); 
        tree_set.add(20); 
        tree_set.add(6); 
        tree_set.add(20); 
        tree_set.add(18); 
        tree_set.add(9); 
        tree_set.add(30); 
  
        // Creating the subset tree 
        TreeSet<Integer> sub_set = new TreeSet<Integer>(); 
  
        // Limiting the values till 5 
        sub_set = (TreeSet<Integer>)tree_set.subSet(6, 30); 
  
        // Creating an Iterator 
        Iterator iterate; 
        iterate = sub_set.iterator(); 
  
        // Displaying the tree set data 
        System.out.println("The resultant values within the sub set: "); 
  
        // Iterating through the subset 
        while (iterate.hasNext()) { 
            System.out.println(iterate.next() + " "); 
        } 
    } 
}
輸出:
The resultant values within the sub set: 
6 
9 
10 
18 
20

程序2

// Java code to illustrate subSet() method when TreeSet 
// contains elements of String type 
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 subset tree 
        TreeSet<String> sub_set = new TreeSet<String>(); 
  
        // Limiting the values till 5 
        sub_set = (TreeSet<String>)tree_set.subSet("4", "TreeSet"); 
  
        // Creating an Iterator 
        Iterator iterate; 
        iterate = sub_set.iterator(); 
  
        // Displaying the tree set data 
        System.out.println("The resultant values within the sub set: "); 
  
        // Iterating through the subset 
        while (iterate.hasNext()) { 
            System.out.println(iterate.next() + " "); 
        } 
    } 
}
輸出:
The resultant values within the sub set: 
4 
Geek 
Geeks 
To


相關用法


注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 TreeSet subSet() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。