Steve Kuo Steve Kuo - 11 months ago 58
Java Question

Java library to get Nth element from a Collection or Iterable

I have a

and need to get the Nth element as returned by its
. I know can iterate while counting through its
elements. Is there a third party library (Google Guava or Apache Commons) that does this?

Answer Source

Guava's Iterators.get(Iterator<T>, int) can help

Advances iterator position + 1 times, returning the element at the positionth position.