What's a good way to model a calendar-like database in Java?
I'm getting data from a CSV file in the format {timestamp}, {data1}, {data2}
.
What I'm looking for is a sane, sensible way to store that data in a data structure, so that I can easily do the following:
- For a given
Date
object, find the correct data associated with that date. - If there is no direct match for the date, find the previous and next items.
The first requirement is easily met by a Hashtable. The second one would be easy if a Hashtable had an inherent order, in which case I could just find the greatest entry that's still below my given date for the previous item, and the following one for the next开发者_StackOverflow.
Since that isn't the case, what's a good data structure to use here?
Use SortedMap.
Use Map.get() to retrieve the exactly matched data. If no data available use SortedMap.tailMap(date) and SortedMap.headMap(date) to retrieve elements greater and less then given date.
精彩评论