I created a class DateTime (which wraps GregorianCalendar). I also created a class Event.
I would like to create a collection of events from which I can retrieve an event by its date.
For instance:
event is of type Event;
date1 and date2 are of type DateTime and also date1.equals(date2);
‘events’ is my event collection.
event.put(date1, event)
will add ‘event’ to the collection so that I can retrieve it by
event.get(date2)
I think of using a TreeMap to implement this event collection because I might want to print all the events sorted by date.
So how to set DateTime to be a key of a TreeMap? What should I add to DateTime? and what else to do?
Thanks.
You just need to have the
DateTimeimplementComparable<DateTime>, something along the lines of:The Comparable interface is documented here.