列表和集合之间的性能和内存分配比较 [英] Performance and Memory allocation comparison between List and Set

查看:55
本文介绍了列表和集合之间的性能和内存分配比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解List和Set在性能,内存分配和可用性方面的比较.

I want to know the comparison between List and Set in terms of performance,memory allocation and usability.

如果我不需要在对象列表中保持唯一性,也不需要维护插入顺序,那么我可以互换使用ArrayList和SortedSet/HashSet吗? 直接使用Collections类而不是列表/集合会好吗?

If i don't have any requirement of keeping the uniqueness in the list of objects, neither required the insertion order to be maintained, Can I use ArrayList and SortedSet/HashSet interchangeably? Will it be good to directly use Collections class instead of even list/set?

P.S.我也不需要列表或设置java提供的特定功能. 我之所以使用列表/集而不是数组,是因为它们可以动态增长,而无需额外的编程工作.

P.S. I also don't have any need for list or set specific functions provided by java. I am using List/Set instead of Array only because they can dynamically grow without extra programming efforts.

推荐答案

如果您不关心顺序,也不删除元素,那么实际上归结为是否需要在此数据结构中查找元素,以及您需要进行这些查询的速度.

If you don't care about the ordering, and don't delete elements, then it really boils down to whether you need to find elements in this data structure, and how fast you need those lookups to be.

HashSet中通过值查找元素是O(1).在ArrayList中,它是O(n).

Finding an element by value in a HashSet is O(1). In an ArrayList, it's O(n).

如果仅使用容器存储一堆独特的对象,并在其末尾(以任何顺序)对其进行迭代,那么可以说ArrayList是一个更好的选择,因为它更简单,更经济.

If you are only using the container to store a bunch of unique objects, and iterate over them at the end (in any order), then arguably ArrayList is a better choice since it's simpler and more economical.

这篇关于列表和集合之间的性能和内存分配比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆