I have a TreeMap, that looks like this:
TreeMap<Instant, HashMap<Type, Double>>
The Instant values are representing hours of a day; for each passed hour a value is stored in my map. Now I would like to get the last 24 elements (so the hours of the passed day) of this map. How could I do that?
Cheers
use TreeMap.tailMap() for it.
You can use the descendingMap
call to get a view on the map which is basically in the reverse order, then take the first 24 entries from that (call iterator
etc). (Guava's Iterables provides helpful methods for limiting an iterable etc.)
EDIT: For example, to get the last 24 elements (in reverse order, and using Guava) you could use:
List<HashMap<Type, Double>> lastValues = Lists.newArrayList
(Iterables.limit(map.descendingMap().values(), 24));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With