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

DALL·E 2
DALL·E 2

OpenAI基于GPT-3模型开发的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

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
c语言中null和NULL的区别
c语言中null和NULL的区别

c语言中null和NULL的区别是:null是C语言中的一个宏定义,通常用来表示一个空指针,可以用于初始化指针变量,或者在条件语句中判断指针是否为空;NULL是C语言中的一个预定义常量,通常用来表示一个空值,用于表示一个空的指针、空的指针数组或者空的结构体指针。

236

2023.09.22

java中null的用法
java中null的用法

在Java中,null表示一个引用类型的变量不指向任何对象。可以将null赋值给任何引用类型的变量,包括类、接口、数组、字符串等。想了解更多null的相关内容,可以阅读本专题下面的文章。

458

2024.03.01

typedef和define区别
typedef和define区别

typedef和define区别在类型检查、作用范围、可读性、错误处理和内存占用等。本专题为大家提供typedef和define相关的文章、下载、课程内容,供大家免费下载体验。

109

2023.09.26

define的用法
define的用法

define用法:1、定义常量;2、定义函数宏:3、定义条件编译;4、定义多行宏。更多关于define的用法的内容,大家可以阅读本专题下的文章。

338

2023.10.11

硬盘接口类型介绍
硬盘接口类型介绍

硬盘接口类型有IDE、SATA、SCSI、Fibre Channel、USB、eSATA、mSATA、PCIe等等。详细介绍:1、IDE接口是一种并行接口,主要用于连接硬盘和光驱等设备,它主要有两种类型:ATA和ATAPI,IDE接口已经逐渐被SATA接口;2、SATA接口是一种串行接口,相较于IDE接口,它具有更高的传输速度、更低的功耗和更小的体积;3、SCSI接口等等。

1126

2023.10.19

PHP接口编写教程
PHP接口编写教程

本专题整合了PHP接口编写教程,阅读专题下面的文章了解更多详细内容。

192

2025.10.17

php8.4实现接口限流的教程
php8.4实现接口限流的教程

PHP8.4本身不内置限流功能,需借助Redis(令牌桶)或Swoole(漏桶)实现;文件锁因I/O瓶颈、无跨机共享、秒级精度等缺陷不适用高并发场景。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

1646

2025.12.29

java接口相关教程
java接口相关教程

本专题整合了java接口相关内容,阅读专题下面的文章了解更多详细内容。

20

2026.01.19

俄罗斯Yandex引擎入口
俄罗斯Yandex引擎入口

2026年俄罗斯Yandex搜索引擎最新入口汇总,涵盖免登录、多语言支持、无广告视频播放及本地化服务等核心功能。阅读专题下面的文章了解更多详细内容。

158

2026.01.28

热门下载

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

精品课程

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

共100课时 | 9.9万人学习

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

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