首頁 > Java > java教程 > 主體

Java 中的樹集

王林
發布: 2024-08-30 15:07:03
原創
527 人瀏覽過

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個建構子。他們是:

  1. TreeSet(): 將建立一個新的空 TreeSet,並依照自然順序進行排序。
  2. TreeSet(Collection extends E> c): 將使用集合 c 中提到的元素建立一個新的 TreeSet,並且它們將根據自然順序進行排序。
  3. TreeSet(Comparator super E> 比較器): 將根據提到的比較器排序建立新的空 TreeSet。
  4. 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(CollectionE> 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:

Java 中的樹集

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中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!