I need to store a set of data structures that are defined by period of time (start, end) and counter for this period that holds some complex calculation results. The simplified definition of the data structure is as follows:
public class CounterBag {
private Period period; // collection key
private Counter counter;
// accessors
// ...
}
the Period is as simple as:
public class Period {
public DateTime start;
public DateTime end;
// accessors
// ...
}
I need to have a collection that holds CounterBag objects defined by distinct Periods.
The collection needs to provide efficient lookup (here is the catch!) by long timeInMillis , so HashMap is not really an option, since I do not want to overwrite equals and hashcode of CounterBag (i need them both). The collection needs to be sorted by Period (by end date). Periods are of flexible duration which is not known to the part that would perform the lookup.
I wonder is there an out-of-the-box collection in java standard API or in some open source library that can help me to solve it? Some kind of sorted set or sorted map that enables implementing an efficient lookup by date. The lookup by date would return a CounterBag with Period the date falls within.
Appreciate your suggestions.
You could use TreeMap as its a Sorted collection (which makes the lookup efficient)
If your periods have regular intervals (which is the simplest form) you don’t need such a collection. You can just have a counter for each interval. e.g. a
int[]