1

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; use Collections.sort(al); and use Collections.binarySearch(al, key) to determine whether I've used an object. 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.

Thanks much.

This is 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?

}
Brian K.
  • 93
  • 8

3 Answers3

6

This begs the question - why not use a data structure that doesn't allow duplicates (e.g. Set)? If you attempt to add a duplicate item, the method will return false and the data structure will remain unchanged.

mre
  • 40,416
  • 33
  • 117
  • 162
6

Instead of using an ArrayList, why wouldn't you use a Set implementation (likely a HashSet)? You'll get constant-time lookup, no sorting needed.

N.B. your objects will need to correctly override hashCode() and equals().

Community
  • 1
  • 1
Matt Ball
  • 332,322
  • 92
  • 617
  • 683
  • I looked up hashList. :( \\ figures it was HashSet. Thanks... trying it out. – Brian K. Jan 06 '12 at 15:07
  • I can accept this in a few minutes. Worked perfectly. Luckily my objects this time are strings so no extra implementation necessary... **waits for click to tick down... – Brian K. Jan 06 '12 at 15:12
3

Make sure the objects have correct equals() and hashCode() methods, and store them in a 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.

Ernest Friedman-Hill
  • 77,245
  • 10
  • 138
  • 182
  • I do need to retain all objects this time, its important to the data I'm spitting out, but good to know. You learn so much and you think you know everything there is to know about Java... and then you don't. :) – Brian K. Jan 06 '12 at 15:14