I have a TreeMap<Date, Integer> and I want to fetch the nth item from that map. What I came up with now is this:
((Integer)myTreeMap.values().toArray()[index]).intValue();
but this feels quite clunky, not to mention what is happening on the heap or with performance?
Is there a concise way to get the nth item from a TreeMap/SortedMap ?
Edit: an alternative is
What about
Tis just a generic type in place of yourMapvalue type.indexis your nth element.As pointed out, that's inefficient for large maps, as internally the
ArrayListconstructor thetoArraymethod is called