📜  JavaTreeSet和SortedSet的区别

📅  最后修改于: 2021-09-14 02:15:04             🧑  作者: Mango

TreeSet 是Java中 SortedSet 接口的实现之一,它使用红黑树进行存储。默认情况下,元素按升序存储。它也可以由在集合创建时提供的 Comparator 进行排序,具体取决于使用的构造函数。

  • 它还实现了 NavigableSet 接口。
  • NavigableSet 扩展了 SortedSet 和 Set 接口。

例子:

Java
// Java program to demonstrate the TreeSet
 
import java.util.*;
 
class Main {
    public static void main(String args[])
    {
        // Creating and adding elements
        TreeSet al = new TreeSet();
 
        // Add elements
        al.add("Welcome");
        al.add("to");
        al.add("Geeks for Geeks");
 
        // Traversing elements
        Iterator itr = al.iterator();
        while (itr.hasNext()) {
            System.out.println(itr.next());
        }
    }
}


Java
// Java program to demonstrate the
// Sorted Set
 
import java.util.*;
 
class Main {
 
    public static void main(String[] args)
    {
 
        // create an instance of sortedset
        SortedSet ts = new TreeSet();
 
        // Adding elements into the TreeSet
        // using add()
        ts.add("Sravan");
        ts.add("Ojaswi");
        ts.add("Bobby");
        ts.add("Rohith");
        ts.add("Gnanesh");
        ts.add("Devi2");
 
        // Adding the duplicate
        // element
        ts.add("Sravan");
 
        // Displaying the TreeSet
        System.out.println(ts);
 
        // Removing items from TreeSet
        // using remove()
        ts.remove("Ojaswi");
 
        // Iterating over Tree set items
        System.out.println("Iterating over set:");
        Iterator i = ts.iterator();
        while (i.hasNext())
            System.out.println(i.next());
    }
}


输出
Geeks for Geeks
Welcome
to

排序集:

SortedSet 接口在扩展 Set 接口的Java.util 包中可用。该接口包含继承自 Set 接口的方法,并添加了一个特性,该特性将存储该接口中的所有元素以排序方式存储。

例子:

Java

// Java program to demonstrate the
// Sorted Set
 
import java.util.*;
 
class Main {
 
    public static void main(String[] args)
    {
 
        // create an instance of sortedset
        SortedSet ts = new TreeSet();
 
        // Adding elements into the TreeSet
        // using add()
        ts.add("Sravan");
        ts.add("Ojaswi");
        ts.add("Bobby");
        ts.add("Rohith");
        ts.add("Gnanesh");
        ts.add("Devi2");
 
        // Adding the duplicate
        // element
        ts.add("Sravan");
 
        // Displaying the TreeSet
        System.out.println(ts);
 
        // Removing items from TreeSet
        // using remove()
        ts.remove("Ojaswi");
 
        // Iterating over Tree set items
        System.out.println("Iterating over set:");
        Iterator i = ts.iterator();
        while (i.hasNext())
            System.out.println(i.next());
    }
}
输出
[Bobby, Devi2, Gnanesh, Ojaswi, Rohith, Sravan]
Iterating over set:
Bobby
Devi2
Gnanesh
Rohith
Sravan

TreeSet 和 SortedSet 的区别

TreeSet

SortedSet

TreeSet is a concrete class. SortedSet is an interface.
TreeSet allows a heterogeneous object. SortedSet allows a heterogeneous object.
TreeSet maintains an object in sorted order. SortedSet maintains an object in sorted order.

Syntax:

TreeSet treeset = new TreeSet();

Syntax:

It can not be instantiated as it is a Interface