最轻量级的 Java 集合

发布于 2024-11-29 06:41:09 字数 223 浏览 0 评论 0原文

如果我要创建一个 Java Collection,并且只想用元素填充它,然后迭代它(事先不知道必要的大小),即我需要的是 Collection.add(E)Collection.iterator(),我应该选择哪个具体类?例如,使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

水波映月 2024-12-06 06:41:09

我应该选择哪个具体类?

我可能会选择 ArrayList链接列表。两者都支持 additerator 方法,并且它们中的任何一个都有相当大的开销。

例如,使用集合而不是列表有什么优势吗?

不,我不会这么说。 (除非你依赖元素的顺序,在这种情况下你必须使用列表,或者想要禁止重复,在这种情况下你应该使用集合。)

(我不明白任何 Set 实现如何击败列表添加/迭代器方法的实现,所以即使我不关心顺序,我也可能会使用列表。)

哪一个的开销最少?

听起来像是这里的微基准测试,但如果我被迫猜测,我会说 ArrayList (或者在特殊情况下可能是 LinkedList ArrayList 需要经常重新分配内存:-)

which concrete class should I choose?

I would probably just go with an ArrayList or a LinkedList. Both support the add and iterator methods, and neighter of them have any considerable overhead.

Is there any advantage to using a Set rather than a List, for example?

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.)

Which one would have the least overhead?

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 :-)

玉环 2024-12-06 06:41:09

不要搭配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 right Collection

  • a List is there for maintaining elements in the order you added them; and if you insert the same element twice it will be kept twice
  • a Set is there for holding one specific element exactly once (uniqueness); order is only relevant for specific implementations (like TreeSet), but still elements that are 'the same' would not be added twice
潦草背影 2024-12-06 06:41:09

仅当您想要对对象进行排序并确保没有“注册”重复元素时,Set 才有意义。否则,ArrayList 就可以了。

但是,如果您也想在迭代时添加元素,则 ArrayBlockingQueue 会更好。

Set is only meaningful if you want to sort your objects and to make sure no duplicate element is 'registered'. Else, an ArrayList is just fine.

However, if you want to add elements while iterating too, an ArrayBlockingQueue is better.

且行且努力 2024-12-06 06:41:09

以下是一些要点,可以帮助您根据需要选择集合 -

  1. List(ArrayList 或 LinkedList)

    • 允许重复值。
    • 已保留广告订单。
  2. 设置

    • 不允许重复值。
    • 不保留插入顺序。

所以根据你的要求 List 似乎是一个合适的选择。

现在介于 ArrayList 和 LinkedList 之间 -

ArrayList 是一个随机访问列表。如果您的频繁操作是检索元素,请使用。

如果您想从列表中添加或删除元素,LinkedList 是最佳选择。

Here are some key points which can help you to choose your collection according to your requirement -

  1. List(ArrayList or LinkedList)

    • Allowed duplicate values.
    • Insertion order preserved.
  2. Set

    • Not allowed duplicate values.
    • Insertion order is not preserved.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文