List ordered by date
Hi, I have to implement a list of objects ordered by date, and I also need to access objects by id. Some objects could have the same date.
How can I do it?
May be a TreeMap and the objects implementing the compareTo(Object o)?
Thanx
[257 byte] By [
txatia] at [2007-11-27 10:14:21]

You didn't tell us enough of your requirements to make a good judgement about it. My immediate idea is a List (ArrayList or LinkedList) in which the objects are ordered by date and a Map (HashMap or TreeMap) that maps ids to objects. Could be overkill, though.
For sorting, if you consider ordering by date the natural ordering of your objects, implementing the Comparable interface is a good choice, as you said. If not, go for a Comparator instead. Whatever you do, you'll probably delegate to Date.compareTo(Date anotherDate).
OleVVa at 2007-7-28 15:32:48 >

I'll try to give more information. This is my pseudocode:
while (true) {
MyObject m = list.getFirst();
Date now = new Date();
if ( m.getTime().after(now) ) {
//PROCESS
} else {
Thread.sleep(1000);
}
}
The point is to sleep until an object has to be executed, and the list is modified constantly (reordering by date). Note that some objects could have the same exact time.
The list will have thousands of elements (a thread for each element is discarted).
Also, I need to access the objects in the list by their ID.
I hope it's clear now.
txatia at 2007-7-28 15:32:48 >

It's somewhat clearer, thanks.
I still think you would like to have 2 data structures (collections), one for access by time and one for access by ID. Obviously, you will have to make sure the exact same objects are in both collections always (I'm using "collection" loosely here, it might be a map even though in Java, a Map is not a Collection).
Did you look at existing tools for your timing requirements? I once used the timer mechanisms of EJB for something similar to what you're doing. I believe there's also an open source Java cron-like implementation out there.
OleVVa at 2007-7-28 15:32:48 >
