Java中的TreeSet被認為是有助於儲存的SortedSet介面的主要實作之一。在此,元素以自然順序或基於顯式比較器的順序設定的方式排序。 TreeSet繼承了AbstractSet類別並實作了NavigableSet介面。儘管它與 HashSet 類似,但它維護順序,而 HashSet 不維護順序。此外,與 HashSet 不同,TreeSet 允許使用 null 元素。有關 TreeSet 的更多詳細資訊將在以下部分中討論。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
TreeSet 的特點
以下是TreeSet的主要功能。
- 不允許重複值。
- 物件依升序排序。
- 不保留插入順序。除此之外,元素根據鍵進行排序。
- 不允許插入異質物件。如果我們嘗試插入,則會在運行時拋出 classCastException。
- 儲存大量排序資料。
- 快速檢索時間。
- 快速存取。
- 不允許使用空元素。
- 新增、搜尋和刪除等操作需要 O(log n) 的時間。
- 在列印n個元素等操作時,時間複雜度會是O(n)。
- 不同步。
- TreeMap 的存在是為了儲存元素。
- 不是線程安全的。
文法:
為了建立TreeSet,先匯入套件java.util.TreeSet。之後,使用語法建立一個 TreeSet:
TreeSet<Integer> num= new TreeSet<>();
登入後複製
由於我們在建立 TreeSet 時沒有提及任何參數,因此元素將按照其自然順序進行排序。即升序。
如已經提到的,可以使用 Comparator 介面來自訂排序。
Java 中 TreeSet 的建構子
Java中的TreeSet有4個建構子。他們是:
-
TreeSet(): 將建立一個新的空 TreeSet,並依照自然順序進行排序。
-
TreeSet(Collection extends E> c): 將使用集合 c 中提到的元素建立一個新的 TreeSet,並且它們將根據自然順序進行排序。
-
TreeSet(Comparator super E> 比較器): 將根據提到的比較器排序建立新的空 TreeSet。
-
TreeSet(SortedSet s):將使用 SortedSet 中提到的元素建立一個新的 TreeSet,其順序與其相同。
Java 中 TreeSet 的方法
TreeSet 中有幾個需要執行的函數。讓我們看看它們是什麼。
-
add(Ee): An element e will be added to the set if it is not present in it.
-
addAll(Collection extends E> c): All the elements in collection c will be added to the set.
-
E ceiling(Ee): The last element which is greater than or equal to the element in the set will be returned.
-
clear(): All the elements in the set will be removed.
-
clone(): A shallow copy will be returned for the TreeSet instance.
-
comparator(): The comparator used in the set will be returned. If natural ordering is used, null will be returned.
-
contains(Objecto): If the set contains the element o, true will be returned.
-
descendingIterator(): An iterator will be returned over the elements in descending order.
-
descendingSet(): A reverse order view will be returned for the elements present in the list.
-
first(): he first or the lowest element in the set will be returned.
-
last(): The last or the largest element in the set will be returned.
-
iterator(): n iterator will be returned over the elements in ascending order.
-
lower(Ee): The greatest element will be returned which is strictly small than the element e which is given. If there is no such element, null will be returned.
-
higher(Ee): The smallest element will be returned which is strictly high than the element e which is given. If there is no such element, null will be returned.
-
isEmpty(): True will be returned if no elements are present.
-
size(): The number of elements in the set will be returned. In other words, cardinality will be returned.
-
pollFirst(): The first or the lowest element in the set will be retrieved and removed. If there are no elements in the set, the null will be returned.
-
pollLast(): The last or the highest element in the set will be retrieved and removed. If there are no elements in the set, the null will be returned.
-
remove(Objecto): If the set contains the element o, it will be removed.
-
subSet(EfromElement, boolean fromInclusive, E toElement, boolean toInclusive): A view will be returned for the portion of the set from the range from Element to toElement.
-
subSet(EfromElement, E toElement): A view will be returned for the portion of the set from the range fromElement(inclusive) to toElement(exclusive).
-
tailSet(EfromElement): view will be returned for the portion of the set where elements are larger than fromElement.
-
tailSet(EfromElement, boolean inclusive): A view will be returned for the portion of the set where elements are larger than fromElement. It is considered if inclusive is true.
Example of TreeSet in Java
Java program to create a tree set with the natural ordering of elements.
import java.util.SortedSet;
import java.util.TreeSet;
//class
public class TreeSetExample {
//main method
public static void main(String[] args) {
// TreeSet <u>fam</u> creation
SortedSet<String> fam = new TreeSet<>();
// Adding new elements to a TreeSet
fam.add("Anna");
fam.add("Adam");
fam.add("Sam");
fam.add("Iza");
//print the treeset
System.out.println("Fam Set : " + fam);
// Trying to add duplicate element <u>Anna</u>
fam.add("Anna");
System.out.println("Added element Anna, Now the treeset is : " + fam);
// Trying to add element <u>Anna</u> with lower case
fam.add("anna");
System.out.println("Added element anna, Now the treeset is : " + fam);
}
}
登入後複製
Output:
A treeset fam is created first. Once it is created, elements are added to it and the whole treeset is printed. After this, an element ‘Anna’ which is already present in the treeset is trying to get added. Since duplicates are not allowed in TreeSet, it is not get added. After that, an element ‘anna’ which is the lower case of the already existing element ‘Anna’ is added. Since it is in lowercase, it gets added to the treeset without any problem.
Conclusion
TreeSet in Java is an implementation of the interface SortedSet that helps in storage where the elements are arranged in natural ordering or based on the comparator. Several aspects of TreeSet is discussed in this document.
以上是Java 中的樹集的詳細內容。更多資訊請關注PHP中文網其他相關文章!