Java 中 TreeMap 和 TreeSet 的相似之處
TreeMap 和 TreeSet 都是 Collection Framework 類的一部分。在它們的實現和工作方式上,存在一些差異,也存在一些相似之處。TreeMap 維護鍵值對,而 TreeSet 沒有此功能。在本文中,我們將討論這兩個 Collection 介面類的相似之處。
Collection 介面
在 Java 中,集合是一個物件,或者為了簡單起見,我們可以稱之為容器,它允許我們將多個物件組合在一個單元中。Collection 介面位於所有集合框架介面的根部。
TreeMap 和 TreeSet 實現以下 Collection 介面的子介面:
Map 介面 - 它以鍵值對的形式儲存其元素。鍵是一個用於獲取和接收與其關聯的值的物件。
Set - 它是 Java Collection 介面的子介面,不允許重複值。它類似於數學集合。
TreeMap
它是一個用於實現 NavigableMap 介面的類。它以樹結構儲存對映的元素。它提供了一種有效的替代方案,可以按排序順序儲存鍵值對。
TreeMap 的一般語法如下:
語法
TreeMap<TypeOfKey, TypeOfValue> nameOfMap = new TreeMap<>();
TreeSet
它是一個用於實現 NavigableSet 介面的類。它以樹結構儲存集合的元素。所有元素都以排序方式儲存,從而減少了檢索時間。
TreeSet 的一般語法如下:
語法
TreeSet<TypeOfSet> nameOfSet = new TreeSet<>();
TreeMap 和 TreeSet 的 Java 程式
示例 1
以下示例說明了 TreeSet 的用法。我們使用了此類的一些內建方法。
import java.util.*;
public class Srtset {
public static void main(String args[]) {
// Creating tree set
TreeSet<String> treeSt = new TreeSet<>();
// Adding elements in tree set
treeSt.add("Tutorix");
treeSt.add("Simply");
treeSt.add("Easy");
treeSt.add("Learning");
treeSt.add("Tutorials");
treeSt.add("Point");
System.out.println("Elements in the given set: " + treeSt);
String frst = treeSt.first();
// to access first element
System.out.println("Accessing First element of the given set: " + frst);
String end = treeSt.last();
// to access last element
System.out.println("Accessing Last element of the given set: " + end);
System.out.println("Accessing subsets of the given set: " + treeSt.subSet("Simply", "Tutorix"));
System.out.println("Accessing first two elements of set: " + treeSt.headSet("Point"));
System.out.println("Accessing last three elements of set: " + treeSt.tailSet("Simply"));
}
}
輸出
Elements in the given set: [Easy, Learning, Point, Simply, Tutorials, Tutorix] Accessing First element of the given set: Easy Accessing Last element of the given set: Tutorix Accessing subsets of the given set: [Simply, Tutorials] Accessing first two elements of set: [Easy, Learning] Accessing last three elements of set: [Simply, Tutorials, Tutorix]
示例 2
以下示例說明了 TreeMap 的實現。我們使用了此類的一些內建方法。
import java.util.*;
public class Srt {
public static void main(String[] args) {
TreeMap<String, Integer> workers = new TreeMap<>();
// Adding elements in the workers map
workers.put("Vaibhav", 4000);
workers.put("Ansh", 3000);
workers.put("Vivek", 1500);
workers.put("Aman", 2000);
workers.put("Tapas", 2500);
// printing details workers map
System.out.println("Elements of the map: ");
for (String unKey : workers.keySet()) {
System.out.println("Name: " + unKey + ", Salary: " + workers.get(unKey));
}
String frstKey = workers.firstKey();
// accessing first key
String lstKey = workers.lastKey();
// accessing last key
System.out.println("Accessing name of first key in Map: " + frstKey);
System.out.println("Accessing name of first key in Map: " + lstKey);
}
}
輸出
Elements of the map: Name: Aman, Salary: 2000 Name: Ansh, Salary: 3000 Name: Tapas, Salary: 2500 Name: Vaibhav, Salary: 4000 Name: Vivek, Salary: 1500 Accessing name of first key in Map: Aman Accessing name of first key in Map: Vivek
TreeMap 和 TreeSet 之間的相似之處
預設情況下,它們的元素按自然排序排序。例如,它們按字典順序儲存字串,按數字順序儲存數字。
由於元素已排序,因此訪問和檢索時間變得更快。由於此出色功能,TreeMap 和 TreeSet 經常用於儲存需要快速搜尋的大量資訊。
不允許空值。
它們定義在“java.util”包內。
兩者都支援 Comparable 介面,可以實現該介面以定義自定義排序順序。
結論
在本文中,我們學習了 Collection Framework 的 Map 和 Set 介面。此外,我們發現了用於實現上述介面的 TreeMap 和 TreeSet 類。最後,我們討論了一些說明這兩個類之間相似之處的要點。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP