Steve Kuo Steve Kuo - 16 days ago 7
Java Question

Java library to get Nth element from a Collection or Iterable

I have a

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

Answer

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

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