List,Set,Map三者的区别

本文深入探讨了Java集合框架中List、Set和Map的核心概念及差异。解析了List作为有序对象集合,允许重复;Set强调元素的唯一性,禁止重复;Map通过键值对存储数据,确保键的唯一性。此外,还对比了它们在处理重复对象、顺序维护及数据检索方面的不同表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

List(对付顺序的好帮手): List接口存储一组不唯一(可以有多个元素引用相同的对象),有序的对象

List in Java provides ordered and indexed collection which may contain duplicates.
Set(注重独一无二的性质): 不允许重复的集合。不会有多个元素引用相同的对象。
Map(用Key来搜索的专家): 使用键值对存储。Map会维护与Key有关联的值。两个Key可以引用相同的对象,但Key不能重复,典型的Key是String类型,但也可以是任何对象。

The Set interface provides an unordered collection of unique objects, i.e. Set doesn’t allow duplicates, while Map provides a data structure based on key-value pair and hashing.

All three List, Set, and Map are interfaces in Java and there are many concrete implementations of them are available in Collection API.
ArrayList and LinkedList are two most popular used List implementation while LinkedHashSet, TreeSet, and HashSet are frequently used Set implementation.

Set vs List vs Map in Java
As I said Set, List and Map are interfaces, which defines core contract e.g. a Set contract says that it can not contain duplicates. Based on our knowledge of List, Set and Map let’s compare them on different metrics.

Duplicate Objects
The main difference between List and Set interface in Java is that List allows duplicates while Set doesn’t allow duplicates. All implementation of Set honor this contract.

While a Map holds two objects per Entry e.g. a key and a value and It may contain duplicate values but keys are always unique. See here for more difference between List and Set data structure in Java.

Order
Another key difference between List and Set is that List is an ordered collection, List’s contract maintains insertion order or element. Set is an unordered collection, you get no guarantee on which order element will be stored.

Though some of the Set implementation e.g. LinkedHashSet maintains order. Also SortedSet and SortedMap e.g. TreeSet and TreeMap maintain a sorting order, imposed by using Comparator or Comparable.

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值