最轻量级的 Java 集合
如果我要创建一个 Java Collection,并且只想用元素填充它,然后迭代它(事先不知道必要的大小),即我需要的是 Collection
和 Collection
,我应该选择哪个具体类?例如,使用 Set
而不是 List
有什么优势吗?哪一个的开销最少?
If I am going to create a Java Collection, and only want to fill it with elements, and then iterate through it (without knowing the necessary size beforehand), i.e. all I need is Collection<E>.add(E)
and Collection<E>.iterator()
, which concrete class should I choose? Is there any advantage to using a Set
rather than a List
, for example? Which one would have the least overhead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我可能会选择
ArrayList
或链接列表
。两者都支持add
和iterator
方法,并且它们中的任何一个都有相当大的开销。不,我不会这么说。 (除非你依赖元素的顺序,在这种情况下你必须使用列表,或者想要禁止重复,在这种情况下你应该使用集合。)
(我不明白任何 Set 实现如何击败列表添加/迭代器方法的实现,所以即使我不关心顺序,我也可能会使用列表。)
听起来像是这里的微基准测试,但如果我被迫猜测,我会说 ArrayList (或者在特殊情况下可能是 LinkedList ArrayList 需要经常重新分配内存:-)
I would probably just go with an
ArrayList
or aLinkedList
. Both support theadd
anditerator
methods, and neighter of them have any considerable overhead.No, I wouldn't say so. (Unless you rely on the order of the elements, in which case you must use a List, or want to disallow duplicates, in which case you should use a Set.)
(I don't see how any Set implementation could beat a list implementation for add / iterator methods, so I'd probably go with a List even if I don't care about order.)
Sounds like micro benchmarking here, but if I'd be forced to guess, I'd say ArrayList (or perhaps LinkedList in coner cases where ArrayLists need to reallocate memory often :-)
不要搭配
Set
。集合和列表根据其用途而有所不同,在选择正确的集合
时,您应该始终考虑列表
用于按照您添加元素的顺序维护元素;如果插入相同的元素两次,它将被保留两次,Set
用于仅保存一个特定元素一次(唯一性); order 仅与特定实现相关(例如TreeSet
),但“相同”的元素仍然不会被添加两次Do not go with a
Set
. Sets and Lists differ according to their purpose, that you should always consider when choosing the rightCollection
List
is there for maintaining elements in the order you added them; and if you insert the same element twice it will be kept twiceSet
is there for holding one specific element exactly once (uniqueness); order is only relevant for specific implementations (likeTreeSet
), but still elements that are 'the same' would not be added twice仅当您想要对对象进行排序并确保没有“注册”重复元素时,
Set
才有意义。否则,ArrayList
就可以了。但是,如果您也想在迭代时添加元素,则 ArrayBlockingQueue 会更好。
Set
is only meaningful if you want to sort your objects and to make sure no duplicate element is 'registered'. Else, anArrayList
is just fine.However, if you want to add elements while iterating too, an
ArrayBlockingQueue
is better.以下是一些要点,可以帮助您根据需要选择集合 -
List(ArrayList 或 LinkedList)
设置
所以根据你的要求 List 似乎是一个合适的选择。
现在介于 ArrayList 和 LinkedList 之间 -
ArrayList 是一个随机访问列表。如果您的频繁操作是检索元素,请使用。
如果您想从列表中添加或删除元素,LinkedList 是最佳选择。
Here are some key points which can help you to choose your collection according to your requirement -
List(ArrayList or LinkedList)
Set
So according to your requirement List seems to be a suitable choice.
Now Between ArrayList and LinkedList -
ArrayList is a random access list. Use if your frequent operation is the retrieval of elements.
LinkedList is the best option if you want to add or remove elements from the list.