处理集合值的复杂性

发布于 2024-10-17 02:19:17 字数 290 浏览 3 评论 0原文

我需要在集合中存储越来越多的对象。在对集合中的每个对象执行操作时,我经常需要检查对象是否已存储。如果尚未存储对象,我会将其添加到集合的末尾。我在进行检查时迭代处理每个对象。

已处理的对象不应从集合中删除,因为当我再次偶然发现它们时,我不想将它们放回处理状态。

因此,我不知道什么系列最适合。 HashSet 有一个恒定时间的“包含”方法,但 List 有更快的方法来迭代其元素,对吧?

什么才是更明智的选择?同时保留两个包含相同节点的不同结构(用于检查的 HashSet 和用于处理的 LinkedList)是否相关?

I need to store a growing large number of objects in a collection. While performing actions of each object of the collection, I regularly need to check whether an object is already stored. If an object is not stored yet I will add it to the end of the collection. I process each object iteratively while doing the checks.

Objects already processed should not be removed from the collection because I do not want put them back to processing when I stumble upon them again.

As a result I do not know what collection may fit best. HashSet has a constant time "contains" method but a List has faster methods to iterate over its elements, right ?

What would be the wiser choice ? Would it be relevant to keep two different structures at a time containing the same nodes, a HashSet for the checks and a LinkedList for the processing ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

一张白纸 2024-10-24 02:19:17

因此,我不知道什么系列最适合。 HashSet 有一个恒定时间的“包含”方法,但 List 有更快的方法来迭代其元素,对吗?

LinkedHashSet

Set 接口的哈希表和链表实现,具有可预测的迭代顺序。此实现与 HashSet 的不同之处在于,它维护一个贯穿其所有条目的双向链表。这个链表定义了迭代顺序,即元素插入到集合中的顺序(insertion-order)

As a result I do not know what collection may fit best. HashSet has a constant time "contains" method but a List has faster methods to iterate over its elements, right ?

How about a LinkedHashSet?

Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order)

天涯离梦残月幽梦 2024-10-24 02:19:17

1)使用ArrayList,而不是LinkedList。 LinkedList 消耗大量内存,并且迭代速度比 ArrayList 慢。

2)我建议使用两种数据结构。例如,由于您无法在迭代集合时添加到集合中(ConcurrentModificationException)

1) Use ArrayList, not LinkedList. LinkedLists consume a lot of memory, and it's slower on iteration than ArrayList.

2) I'd suggest to use two data structures. E.g. for the sake of you being unable to add to a collection wile iterating through it (ConcurrentModificationException)

薔薇婲 2024-10-24 02:19:17

嗯,看来您对您的收藏的两种观点感兴趣。

  1. 类似于队列的视图,将内容添加到末尾并在前面检查它们。
  2. A contains check

所有这些操作在不同类型的堆中都得到很好的支持,例如 java.util.PriorityQueue

Well, it seems you are interested in two views on your collection.

  1. A queue like view, adding things to the end and inspecting them at the front.
  2. A contains check

All those operations are well supported in different kinds of heaps, e.g. java.util.PriorityQueue

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