Use of DataStructure for this scenario
I am having data in the following form -
01/04/2011 - {00:00,1.0} ; {01:00,2.0} ; {02:00,1.5} ;{04:00,2.3}
02/04/2011 - {00:00,2.0} ; {01:00,2.1} ; {02:00,1.5} ;{04:00,2.3}
03/04/2011 - {00:00,3.0} ; {01:00,2.0} ; {02:00,1.6} ;{04:00,2.3}
04/04/2011 - {00:00,1.0} ; {01:00,2.5} ; {02:00,1.57} ;{04:00,2.3}
05/04/2011 - {00:00,1.9} ; {01:00,2.7} ; {02:00,1.5} ;{04:00,2.3}
06/04/2011 - {00:00,1.08} ; {01:00,2.02} ; {02:00,1.9} ;{04:00,2.3}
07/04/2011 - {00:00,1.7} ; {01:00,2.0} ; {02:00,1.10} ;{04:00,2.3}
I have to store them in some DataStructure so that i can access them using the dates as the key.Also for the particu开发者_如何学Golar day the {k,v} pairs should be ordered in the way they get inserted.
Pls suggest which is the best DataStructure to use in this scenario with the reason why it should be used. Thanks in advance!
Sounds exactly like a Map<Date,List<Record>>
to me. If you use a HashMap
then looking up a given date will be an O(1) operation. There's no particular ordering using this approach.
Alternatively you could use a LinkedHashMap
then ordering can be preserved which might be useful if you need to go through the records in date order.
Looks like a MultiMap to me.
MultiMap<Date, SomeRecord> records = ArrayListMultiMap.create();
records.put(..., ...);
I think the best solution is this structure: http://download.oracle.com/javase/1.4.2/docs/api/java/util/LinkedHashMap.html
The entries are saved in order they was inserted. There is another good point - due the key is hashed the search in the list is done faster.
From your sample data it appears that the values for each date is just a collection of numbers for each hour. So you could simply use Map<Date, Float>
if there's always just one date and time per number. E.g. "01/04/2011 00:00" is "1.0" and "01/04/2011 01:00" is "2.0". You could keep them in order with a TreeMap<Date, Float>
and pull just the ranges of dates you need with the subMap()
method.
精彩评论