处理集合值的复杂性
我需要在集合中存储越来越多的对象。在对集合中的每个对象执行操作时,我经常需要检查对象是否已存储。如果尚未存储对象,我会将其添加到集合的末尾。我在进行检查时迭代处理每个对象。
已处理的对象不应从集合中删除,因为当我再次偶然发现它们时,我不想将它们放回处理状态。
因此,我不知道什么系列最适合。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
LinkedHashSet
?How about a
LinkedHashSet
?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)
嗯,看来您对您的收藏的两种观点感兴趣。
所有这些操作在不同类型的堆中都得到很好的支持,例如 java.util.PriorityQueue
Well, it seems you are interested in two views on your collection.
All those operations are well supported in different kinds of heaps, e.g. java.util.PriorityQueue