0

0

Java中TreeMap和TreeSet的相似之处

王林

王林

发布时间:2023-08-20 21:25:07

|

1114人浏览过

|

来源于tutorialspoint

转载

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接口 − 它以键值对的形式存储元素。键是一个对象,用于获取和接收与之关联的值。

    立即学习Java免费学习笔记(深入)”;

  • 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的一般语法如下 -

LongShot
LongShot

LongShot 是一款 AI 写作助手,可帮助您生成针对搜索引擎优化的内容博客。

下载

语法

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速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
java
java

Java是一个通用术语,用于表示Java软件及其组件,包括“Java运行时环境 (JRE)”、“Java虚拟机 (JVM)”以及“插件”。php中文网还为大家带了Java相关下载资源、相关课程以及相关文章等内容,供大家免费下载使用。

825

2023.06.15

java正则表达式语法
java正则表达式语法

java正则表达式语法是一种模式匹配工具,它非常有用,可以在处理文本和字符串时快速地查找、替换、验证和提取特定的模式和数据。本专题提供java正则表达式语法的相关文章、下载和专题,供大家免费下载体验。

724

2023.07.05

java自学难吗
java自学难吗

Java自学并不难。Java语言相对于其他一些编程语言而言,有着较为简洁和易读的语法,本专题为大家提供java自学难吗相关的文章,大家可以免费体验。

731

2023.07.31

java配置jdk环境变量
java配置jdk环境变量

Java是一种广泛使用的高级编程语言,用于开发各种类型的应用程序。为了能够在计算机上正确运行和编译Java代码,需要正确配置Java Development Kit(JDK)环境变量。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

396

2023.08.01

java保留两位小数
java保留两位小数

Java是一种广泛应用于编程领域的高级编程语言。在Java中,保留两位小数是指在进行数值计算或输出时,限制小数部分只有两位有效数字,并将多余的位数进行四舍五入或截取。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

398

2023.08.02

java基本数据类型
java基本数据类型

java基本数据类型有:1、byte;2、short;3、int;4、long;5、float;6、double;7、char;8、boolean。本专题为大家提供java基本数据类型的相关的文章、下载、课程内容,供大家免费下载体验。

445

2023.08.02

java有什么用
java有什么用

java可以开发应用程序、移动应用、Web应用、企业级应用、嵌入式系统等方面。本专题为大家提供java有什么用的相关的文章、下载、课程内容,供大家免费下载体验。

429

2023.08.02

java在线网站
java在线网站

Java在线网站是指提供Java编程学习、实践和交流平台的网络服务。近年来,随着Java语言在软件开发领域的广泛应用,越来越多的人对Java编程感兴趣,并希望能够通过在线网站来学习和提高自己的Java编程技能。php中文网给大家带来了相关的视频、教程以及文章,欢迎大家前来学习阅读和下载。

16881

2023.08.03

php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

74

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Go语言教程-全程干货无废话
Go语言教程-全程干货无废话

共100课时 | 9.4万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号