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


Java SortedSet Interface用法及代碼示例

java.util 包中的 SortedSet 接口擴展了 collection framework 中的 Set 接口。它是一個實現數學集的接口。該接口包含繼承自Set接口的方法,並增加了一個函數,可以將該接口中的所有元素以排序的方式存儲。

Set-TreeSet-SortedSet-In-Java-Collection

在上圖中,可導航集擴展了排序集接口。由於集合不保留插入順序,因此可導航集合接口提供了在集合中導航的實現。實現可導航集的類是 TreeSet,它是 self-balancing 樹的實現。因此,這個接口為我們提供了一種瀏覽這棵樹的方法。

聲明: SortedSet 接口聲明為:

public interface SortedSet extends Set

排序集的示例:


// Java program to demonstrate the 
// Sorted Set 
import java.util.*; 
  
class SortedSetExample{ 
  
    public static void main(String[] args) 
    { 
        SortedSet<String> ts 
            = new TreeSet<String>(); 
  
        // Adding elements into the TreeSet 
        // using add() 
        ts.add("India"); 
        ts.add("Australia"); 
        ts.add("South Africa"); 
  
        // Adding the duplicate 
        // element 
        ts.add("India"); 
  
        // Displaying the TreeSet 
        System.out.println(ts); 
  
        // Removing items from TreeSet 
        // using remove() 
        ts.remove("Australia"); 
        System.out.println("Set after removing "
                           + "Australia:" + ts); 
  
        // Iterating over Tree set items 
        System.out.println("Iterating over set:"); 
        Iterator<String> i = ts.iterator(); 
        while (i.hasNext()) 
            System.out.println(i.next()); 
    } 
} 
輸出:
[Australia, India, South Africa]
Set after removing Australia:[India, South Africa]
Iterating over set:
India
South Africa

注意:SortedSet 的所有元素必須實現類似的接口(或被指定的比較器接受)並且所有這些元素必須是相互可比較的。相互比較隻是意味著兩個對象互相接受對方作為其compareTo 方法的參數。

創建SortedSet對象

由於 SortedSet 是 interface ,因此無法創建 SortedSet 類型的對象。我們總是需要一個擴展這個列表的類來創建一個對象。而且,在Java 1.5中引入Generics之後,可以限製SortedSet中可以存儲的對象類型。該類型安全集可以定義為:

// Obj is the type of the object to be stored in SortedSet
SortedSet<Obj> set = new TreeSet<Obj> ();

對SortedSet執行各種操作

由於SortedSet 是一個接口,因此它隻能與實現該接口的類一起使用。 TreeSet 是實現SortedSet 接口的類。現在,讓我們看看如何對 TreeSet 執行一些常用操作。

1. 添加元素:為了向 SortedSet 添加元素,我們可以使用SortedSet add()但是,TreeSet 中不保留插入順序。在內部,對於每個元素,都會對值進行比較並按升序排序。我們需要注意的是,不允許有重複的元素,並且所有重複的元素都會被忽略。而且,SortedSet 不接受 Null 值。


// Java code to demonstrate 
// the working of SortedSet 
import java.util.*; 
  
class GFG { 
  
    public static void main(String[] args) 
    { 
        SortedSet<String> ts 
            = new TreeSet<String>(); 
  
        // Elements are added using add() method 
        ts.add("A"); 
        ts.add("B"); 
        ts.add("C"); 
        ts.add("A"); 
  
        System.out.println(ts); 
    } 
} 
輸出:
[A, B, C]

2. 訪問元素:添加元素後,如果我們希望訪問元素,我們可以使用內置方法,例如SortedSet contains(),SortedSet contains(),SortedSet last(), 等等。


// Java code to demonstrate 
// the working of SortedSet 
  
import java.util.*; 
class GFG { 
  
    public static void main(String[] args) 
    { 
        SortedSet<String> ts 
            = new TreeSet<String>(); 
  
        // Elements are added using add() method 
        ts.add("A"); 
        ts.add("B"); 
        ts.add("C"); 
        ts.add("A"); 
  
        System.out.println("Sorted Set is " + ts); 
  
        String check = "D"; 
  
        // Check if the above string exists in 
        // the SortedSet or not 
        System.out.println("Contains " + check 
                           + " " + ts.contains(check)); 
  
        // Print the first element in 
        // the SortedSet 
        System.out.println("First Value " + ts.first()); 
  
        // Print the last element in 
        // the SortedSet 
        System.out.println("Last Value " + ts.last()); 
    } 
} 
輸出:
Sorted Set is [A, B, C]
Contains D false
First Value A
Last Value C

3. 刪除值:可以使用以下命令從 SortedSet 中刪除這些值SortedSet remove().


// Java code to demonstrate 
// the working of SortedSet 
  
import java.util.*; 
class GFG{ 
  
    public static void main(String[] args) 
    { 
        SortedSet<String> ts 
            = new TreeSet<String>(); 
  
        // Elements are added using add() method 
        ts.add("A"); 
        ts.add("B"); 
        ts.add("C"); 
        ts.add("B"); 
        ts.add("D"); 
        ts.add("E"); 
  
        System.out.println("Initial TreeSet " + ts); 
  
        // Removing the element b 
        ts.remove("B"); 
  
        System.out.println("After removing element " + ts); 
    } 
} 
輸出:
Initial TreeSet [A, B, C, D, E]
After removing element [A, C, D, E]

4. 迭代 SortedSet:有多種方法可以迭代 SortedSet。最著名的是使用增強的 for 循環。


// Java code to demonstrate 
// the working of SortedSet 
   
import java.util.*; 
class GFG 
 {  
    public static void main(String[] args) 
    { 
        SortedSet<String> ts 
            = new TreeSet<String>(); 
   
        // Elements are added using add() method 
        ts.add("C"); 
        ts.add("D"); 
        ts.add("E"); 
        ts.add("A"); 
        ts.add("B"); 
        ts.add("Z"); 
   
        // Iterating though the SortedSet 
        for (String value : ts) 
            System.out.print(value 
                             + ", "); 
        System.out.println(); 
    } 
} 
輸出:
A, B, C, D, E, Z,

實現SortedSet接口的類是TreeSet。

樹集:在集合框架中實現的 TreeSet 類是 SortedSet 接口和 SortedSet 擴展的實現設置接口。它的行為就像一個簡單的集合,不同之處在於它以排序的格式存儲元素。 TreeSet采用樹形數據結構進行存儲。對象按升序存儲。但是我們可以使用方法按降序迭代TreeSet descendingIterator()。讓我們看看如何使用此類創建有序集對象。


// Java program to demonstrate the 
// creation of SortedSet object using 
// the TreeSet class 
  
import java.util.*; 
  
class GFG { 
  
    public static void main(String[] args) 
    { 
        SortedSet<String> ts 
            = new TreeSet<String>(); 
  
        // Adding elements into the TreeSet 
        // using add() 
        ts.add("India"); 
        ts.add("Australia"); 
        ts.add("South Africa"); 
  
        // Adding the duplicate 
        // element 
        ts.add("India"); 
  
        // Displaying the TreeSet 
        System.out.println(ts); 
  
        // Removing items from TreeSet 
        // using remove() 
        ts.remove("Australia"); 
        System.out.println("Set after removing "
                           + "Australia:" + ts); 
  
        // Iterating over Tree set items 
        System.out.println("Iterating over set:"); 
        Iterator<String> i = ts.iterator(); 
        while (i.hasNext()) 
            System.out.println(i.next()); 
    } 
} 
輸出:
[Australia, India, South Africa]
Set after removing Australia:[India, South Africa]
Iterating over set:
India
South Africa

Methods of SortedSet Interface

以下是 SortedSet 接口中存在的方法。這裏, “*” 表示這些方法是 Set interface 的一部分。

方法 說明
SortedSet add() 此方法用於將特定元素添加到集合中。僅當指定元素尚不存在於集合中時,該函數才添加該元素,否則,如果該元素已存在於集合中,則該函數返回 False。
SortedSet addAll() 此方法用於將提到的集合中的所有元素追加到現有集合中。這些元素是隨機添加的,不遵循任何特定順序。
SortedSet clear() 該方法用於刪除集合中的所有元素,但不刪除集合。該集的參考仍然存在。
TreeSet comparator() 此方法返回用於對該集合中的元素進行排序的比較器,如果該集合使用其元素的自然排序,則返回 null。
SortedSet contains() 該方法用於檢查Set中是否存在特定元素。
SortedSet containsAll() 此方法用於檢查集合是否包含給定集合中存在的所有元素。如果集合包含所有元素,則此方法返回 true;如果缺少任何元素,則返回 false。
SortedSet first() 此方法返回該集合中存在的第一個(最低)元素。
SortedSet hashCode() 此方法用於獲取此 Set 實例的 hashCode 值。它返回一個整數值,該值是該 Set 實例的 hashCode 值。
SortedSet headSet() 此方法返回小於排序集中存在的元素的元素。
SortedSet isEmpty() 此方法用於檢查SortedSet是否為空。
SortedSet last() 此方法返回集合中存在的最後一個(最高)元素。
SortedSet remove() 該方法用於從集合中刪除給定的元素。如果指定的元素存在於 Set 中,則此方法返回 True,否則返回 False。
SortedSet removeAll() 此方法用於從集合中刪除集合中存在的所有元素。如果此集合因調用而發生更改,則此方法返回 true。
SortedSet retainAll() 此方法用於保留給定集合中提到的集合中的所有元素。如果此集合因調用而發生更改,則此方法返回 true。
SortedSet size() 該方法用於獲取集合的大小。這將返回一個整數值,表示元素的數量。
SortedSet subSet() 此方法從包含 element1 和 element2 之間的元素的集合中返回已排序的子集。
SortedSet tailSet() 此方法返回大於或等於排序集中存在的元素的元素。
SortedSet size() 此方法用於生成與 Set 相同元素的數組。


相關用法


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