the treemap and treeset, both are the part of collection framework classes. there exist a few differences as well as a few similarities in their implementation and working. the treemap maintains key-value pair on the other hand the treeset does not have this feature. in this article, we will discuss the similarities between both classes of collection interface.
collection interfacein java, collection is an object or we can say a container for simplicity that allows us to group several numbers of objects in a single unit. the collection interface is present at the root of all collection framework interfaces.
the following sub-interfaces of collection interface are implemented by treemap and treeset −
map接口 − 它以键值对的形式存储元素。键是一个对象,用于获取和接收与之关联的值。
set − it is the sub interface of java collection interface that doesn’t allow duplicate values. it is similar to mathematical set.
treemap 的翻译为:树图it is a class that is used to implement navigablemap interface. it stores the elements of the map in a tree structure. it provides an efficient alternative to store the key-value pairs in sorted order.
treemap的一般语法如下所示−
语法treemap<typeofkey, typeofvalue> nameofmap = new treemap<>();
treeset它是一个用于实现navigableset接口的类。它将集合的元素存储在一棵树结构中。所有元素都以排序的方式存储,从而减少检索时间。
treeset的一般语法如下 -
语法treeset<typeofset> nameofset = new treeset<>();
java treemap和treeset的程序example 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: easyaccessing last element of the given set: tutorixaccessing subsets of the given set: [simply, tutorials]accessing first two elements of set: [easy, learning]accessing last three elements of set: [simply, tutorials, tutorix]
example 2 的中文翻译为:示例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: 2000name: ansh, salary: 3000name: tapas, salary: 2500name: vaibhav, salary: 4000name: vivek, salary: 1500accessing name of first key in map: amanaccessing name of first key in map: vivek
similarities between treemap and treesetby default, their elements are sorted by natural ordering. for example, they store strings in dictionary order and numerics in numerical order.
由于元素已经排序,访问和检索时间变得更快。由于这个优秀的特性,treemap和treeset经常被用来存储需要快速搜索的大量信息。
null values are not allowed.
they are defined inside ‘java.util’ package.
both support comparable interface that can be implemented to define a custom sorting order.
结论在本文中,我们学习了集合框架的map和set接口。同时,我们还了解了用于实现上述接口的treemap和treeset类。最后,我们讨论了一些解释这些类之间相似性的要点。
以上就是java中treemap和treeset的相似之处的详细内容。