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