简体   繁体   English

Java:有效地跟踪使用过的对象

[英]Java: Effeciently keep track of used objects

I have a program that collects objects over time. 我有一个随时间推移收集对象的程序。 Those objects are often, but not always duplicates of objects the program has already received. 这些对象经常但并非总是与程序已经接收到的对象重复。 The number of unique objects can sometimes be up in the tens of thousands. 唯一对象的数量有时可以达到数万个。 As my lists grow, it takes more time to identify whether an object has appeared or not before. 随着清单的增加,需要花费更多的时间来确定对象是否曾出现过。

My current method is to store everything in an ArrayList, al; 我当前的方法是将所有内容存储在ArrayList中; use Collections.sort(al); 使用Collections.sort(al); and use Collections.binarySearch(al, key) to determine whether I've used an object. 并使用Collections.binarySearch(al,key)来确定我是否使用过一个对象。 Everytime I come across a new object I have to insert and sort however. 每次遇到新对象时,我都必须插入并排序。

I'm wondering if there's just a better way to do this. 我想知道是否有更好的方法可以做到这一点。 Contains tends to slow up too quickly. 包含物往往会太慢地放慢速度。 I'm looking for something as close to O(1) as possible. 我正在寻找尽可能接近O(1)的东西。

Thanks much. 非常感谢。

This is java. 这是java。 For the purpose of understanding what I'm talking about, I basically need a method that does this: 为了理解我在说什么,我基本上需要一种执行此操作的方法:

public boolean objectAlreadyUsed(Object o) {
  return \\ Have we seen this object already?

}

This begs the question - why not use a data structure that doesn't allow duplicates (eg Set )? 这就引出了一个问题-为什么不使用不允许重复的数据结构(例如Set )? If you attempt to add a duplicate item, the method will return false and the data structure will remain unchanged. 如果您尝试添加重复项,则该方法将返回false ,并且数据结构将保持不变。

Instead of using an ArrayList , why wouldn't you use a Set implementation (likely a HashSet )? 而不使用ArrayList ,为什么不使用Set实现(可能是HashSet )呢? You'll get constant-time lookup , no sorting needed. 您将获得固定时间的查询 ,而无需进行排序。

NB your objects will need to correctly override hashCode() and equals() . 注意,您的对象将需要正确覆盖hashCode()equals()

Make sure the objects have correct equals() and hashCode() methods, and store them in a HashSet . 确保对象具有正确的equals()hashCode()方法,并将它们存储在HashSet Lookup then becomes constant time. 查找然后变为恒定时间。

If retaining unwanted objects becomes an issue, by the way, you could consider using one of the many WeakHashSet implementations available on the Internet -- it will hold the objects but still allow them to be garbage collected if necessary. 顺便说一句,如果保留不想要的对象成为一个问题,您可以考虑使用Internet上许多WeakHashSet实现中的一种-它可以保存对象,但在必要时仍允许对其进行垃圾回收。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM