Java中TreeMap和TreeSet的相似之处

2023年 8月 28日 39.5k 0

Java中TreeMap和TreeSet的相似之处

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 Interface

In 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 nameOfMap = new TreeMap();

登录后复制

TreeSet

它是一个用于实现NavigableSet接口的类。它将集合的元素存储在一棵树结构中。所有元素都以排序的方式存储,从而减少检索时间。

TreeSet的一般语法如下 -

语法

TreeSet 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 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]

登录后复制

Example 2

的中文翻译为:

示例2

下面的示例说明了TreeMap的实现。我们使用了这个类的一些内置方法。

import java.util.*;
public class Srt {
public static void main(String[] args) {
TreeMap 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

登录后复制

Similarities between TreeMap and TreeSet

  • By 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的相似之处的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论