1

We have a collection of objects, each object has an integer ID and a timestamp. We want to be able to search for duplicates and update the collection based on the ID.

But we also want to be able to take a "slice" of the collection, for instance finding every object with a timestamp after a given time. So we also want to sort on the timestamp.

We're using a TreeMap, which at first seemed to give us what we wanted. But because the TreeMap (and everything deriving from SortedSet) only uses compareTo() and ignores the equals() method, we find that searching for duplicates based on ID doesn't work. Our compareTo() method tries to allow for both conditions (searching on ID OR timestamp) but ultimately is large and ugly and doesn't actually work. :)

This collection could grow very large, so of course we want as fast as possible searching / sorting / inserting.

2
  • Is a duplicate an Object that has the same id AND timestamp as another object? Commented Sep 7, 2011 at 6:01
  • Sorry, should have specified - an Object is a duplicate if it has the same ID. Commented Sep 7, 2011 at 6:31

1 Answer 1

1

You could use two TreeMaps, one that maps ID to objects, and one that maps timestamps to objects.

Then you can easily find an object based on it's id, or on it's timestamp. You can also get a set of objects with have a timestamp in a specific range (as you already know).

The drawback is obviously that you have to remove objects from both collections. This however shouldn't be that bad, since each object knows both it's id and its timestamp, so if you want to remove by timestamp, you get the id for free, and you're just forced to do one more log-operation.

Wrap them up in a collection of your own if you like.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the suggestion! We're attempting to use this idea now, but we're having trouble setting up a TreeMap that uses the timestamp as the Key, since timestamps aren't guaranteed to be unique. Tried using a TreeMap such as TreeMap<Date, List<Object>>, but that's getting really ugly really fast, and we're still fighting it to make it "work".
Ah, yes. That may be a consern. If I were you I would start looking at Guava or Apache commons by now if I were you. There should be some TreeMultiMap which should suit your needs.
Thanks - we're going to do this, using the Apache TreeMultiMap. We were trying to stay away from too many 3rd party libraries, but in this case it seems better than reinventing the wheel.
@Phillip, staying away from a mess of 3rd party dependencies is a good way imo, Apache Commons or Guava are quite standard however, and does not in turn require anything that I know of, so I think you're doing the right thing here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.