当前位置:网站首页>【转】Collection set map vector list 的区别和联系
【转】Collection set map vector list 的区别和联系
2022-04-21 20:02:00 【delacrxoix_xu】
数组
数组和其它容器的区别主要有三方面:效率,类型,和保存基本类型的能力.在Java中,数组是一种效率很高的存储和随机访问对象引用序列的方式.数组是一个简单的线性序列,因此访问速度很快,但也损失了其它一些特性.创建一个数组对象后,大小就固定了,如果空间不够,通常是再创建一个数组,然后把旧数组中的所有引用移到新数组中.数组可可以保存基本类型,容器不行.
容器类不以具体的类型来处理对象,而是将所有的对象都以Object类型来处理,所以我们可以只创建一个容器,任意的Java对象都可以放进去.容器类可以使用包装类(Integer,Double等),以便把基本类型放入其中. List Set Map 都可以自动调整容量,数组不能.
Collection表示一组对象,这些对象也称为collection的元素。一些 collection允许有重复的元素,而另一些则不允许。一些collection是有序的,而另一些则是无序的。JDK中不提供此接口的任何直接实现,它提供更具体的子接口(如 Set 和 List)实现.
Map 将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射一个值.Map 接口提供三种collection视图,允许以键集、值集合或键值映射关系集的形式查看某个映射的内容。某些映射实现可明确保证其顺序,如 TreeMap(有序) 类;某些映射实现则不保证顺序,如 HashMap(无序) 类。Map可以像数组那样扩展成多维数组,只要把每个值也做成一个Map就行了.
Collection和Map是Java容器中的两种基本类型. 区别在于容器中每个位置保存的元素个数.Collection每个位置只能保存一个元素,包括List和Set.其中List以进入的顺序保存一组元素; 而Set中的元素不能重复.ArrayList是一种List,HashSet是一种Set,将元素添加入任意Collection都可以使用add() 方法.Map保存的是健值对.使用put()为Map添加元素,它需要一个健和一个值作参数.
ArrayList和LinkedList都实现了List接口,ArrayList底层由数组支持LinkedList由双向链表支持,因此,如果经常在表中插入或删除元素LinkedList比较适合,如果经常查询ArrayList比较适合.
Set的实现有TreeSet,HashSet,LinkedHashSet,HashSet查询速度最快,LinkedHashSet保持元素插入次序,TreeSet基于TreeMap,生成一个总是处于排序状态的Set.
Collection<--List<--Vector
Collection<--List<--ArrayList
Collection<--List<--LinkedList
Collection<--Set<--HashSet
Collection<--Set<--HashSet<--LinkedHashSet
Collection<--Set<--SortedSet<--TreeSet
Vector : 基于Array的List,其实就是封装了Array所不具备的一些功能方便我们使用,它不可能走入Array的限制。性能也就不可能超越Array。所以,在可能的情况下,我们要多运用Array。另外很重要的一点就是Vector“sychronized”的,这个也是Vector和ArrayList的唯一的区别。
ArrayList:同Vector一样是一个基于Array上的链表,但是不同的是ArrayList不是同步的。所以在性能上要比Vector优越一些,但是当运行到多线程环境中时,可需要自己在管理线程的同步问题。
LinkedList:LinkedList不同于前面两种List,它不是基于Array的,所以不受Array性能的限制。它每一个节点(Node)都包含两方面的内容:1.节点本身的数据(data);2.下一个节点的信息(nextNode)。所以当对LinkedList做添加,删除动作的时候就不用像基于Array的List一样,必须进行大量的数据移动。只要更改nextNode的相关信息就可以实现了。这就是LinkedList的优势。
List总结:
1. 所有的List中只能容纳单个不同类型的对象组成的表,而不是Key-Value键值对。例如:[ tom,1,c ];
2. 所有的List中可以有相同的元素,例如Vector中可以有 [ tom,koo,too,koo ];
3. 所有的List中可以有null元素,例如[ tom,null,1 ];
4. 基于Array的List(Vector,ArrayList)适合查询,而LinkedList(链表)适合添加,删除操作。
HashSet:虽然Set同List都实现了Collection接口,但是他们的实现方式却大不一样。List基本上都是以Array为基础。但是Set则是在HashMap的基础上来实现的,这个就是Set和List的根本区别。HashSet的存储方式是把HashMap中的Key作为Set的对应存储项。看看HashSet的add(Object obj)方法的实现就可以一目了然了。
public boolean add(Object obj)
{
return map.put(obj, PRESENT) == null;
}
这个也是为什么在Set中不能像在List中一样有重复的项的根本原因,因为HashMap的key是不能有重复的。
LinkedHashSet:HashSet的一个子类,一个链表。
TreeSet:SortedSet的子类,它不同于HashSet的根本就是TreeSet是有序的。它是通过SortedMap来实现的。
Set总结:
1. Set实现的基础是Map(HashMap);
2. Set中的元素是不能重复的,如果使用add(Object obj)方法添加已经存在的对象,则会覆盖前面的对象;
版权声明
本文为[delacrxoix_xu]所创,转载请带上原文链接,感谢
https://blog.csdn.net/delacroix_xu/article/details/5774544
边栏推荐
- webrtc入门:3.使用enumerateDevices获取设备中可用的流媒体
- 表面点云法线
- 开源许可证热门及冷门问题接地气探讨
- Code out efficiency Chapter 7 concurrency and multithreading
- 80.(leaflet篇)leaflet调用geoserver发布的postgis数据图层
- [gradle] problem analysis + download and installation + environment configuration + installation verification
- 你的独立站转化率低?三个小技巧帮助提高转化率
- Instructions for Jerry's reset IO maintenance level [chapter]
- Know that Chuangyu issued a heavy strategic plan to build a practical defense system for continuous exchange of fire
- Digital business cloud community property platform system solution - easy property management and leveraging potential business opportunities
猜你喜欢

阿里IOT
![[2021] number of effective sequences programmed by Tencent autumn recruitment technology post](/img/c2/1246796e2e1e228426788230d0e8ec.png)
[2021] number of effective sequences programmed by Tencent autumn recruitment technology post

Cuda02 - memory access optimization and unified memory

Discussion on the hot and cold issues of open source license grounding gas

Xinguan is merciless, human beings have feelings, Xinlong agriculture ensures people's livelihood and jointly fights the epidemic -- condolences to the front line of fighting the epidemic, love the el

开源许可证热门及冷门问题接地气探讨

Changan dark blue c385 product information exposure aims at 200000 level, and the number one target is model 3!

MySQL修改root用户密码

Yijia announced that its products will enter oppo stores in the future and will launch the group's new resources

Meaning of stripe in image
随机推荐
Use of complex function in MATLAB
【verbs】使用ibverbs api注意事项|libibverbs 中 fork() 支持的状态如何?
05. Prototype mode
What exactly is a vector?
HW - new employee examination - traversal
【玩转Lighthouse】使用腾讯云轻量实现微信支付业务
URL to download network resources
测试while(u--);和while(u)u--;的区别
Meaning of stripe in image
SVPWM模块为什么会出现扇区判断错误?
Notes on checkpoint mechanism in flick
PyCharm failed to create JVM
Publicity of the winners of the 9th "Datang Cup" National College Students' mobile communication 5g technology competition
MFC CComboBox 使用例子
【 summer internship 】
如何判断Int型值的第nbit位是否是1还是0
Digital business cloud: analyze the current situation of enterprise procurement management and promote the optimization and upgrading of enterprise procurement mode
Flinkx deployment
How to let the back door of the network card kill a system and let you know how powerful the network card is
Fiddler's solution to the failure of capturing PC wechat applet