You can use steam and skip n-1 elements and take the first as below:
tmap.entrySet().stream().skip(n-1).findFirst();
. Details with data set examples:
TreeMap<Date, Integer> tmap = new TreeMap<Date, Integer>();
tmap.put(new Date(2014, 1, 1), 0);
tmap.put(new Date(2015, 1, 1), 1);
tmap.put(new Date(2016, 1, 1), 2);
tmap.put(new Date(2017, 1, 1), 3);
tmap.put(new Date(2018, 1,1 ), 4);
System.out.println(tmap);
// Let's find the nth elements i.e n = 3;
int n = 3;
System.out.println(" " + n + " elements: ");
System.out.println(tmap.entrySet().stream().skip(n-1).findFirst());
Output as follow:
{Sun Feb 01 00:00:00 IST 3914=0, Mon Feb 01 00:00:00 IST 3915=1, Tue Feb 01 00:00:00 IST 3916=2, Thu Feb 01 00:00:00 IST 3917=3, Fri Feb 01 00:00:00 IST 3918=4}
3 elements:
Optional[Tue Feb 01 00:00:00 IST 3916=2]
java.util.TreeMap.Values
type to keep the values that does not add support for index-based access. Similarly for the key Set. They do make it easy though to get values for keys above/below thresholds, maybe you can try to use that instead of n-th value? – Vet