Take the 2-minute tour ×  If I have a collection, such as
Collection<String> strs, how can I get the first item out? I
could just call an Iterator, take its first next(), then throw
the Iterator away. Is there a less wasteful way to do it?
share|improve this question Of course there may be a better way
to access the first element if you know the implementing
container class... –  Rooke Nov 4 '09 at 2:32  8 Answers 8  up
vote 51 down vote accepted  Iterables.get(yourC, indexYouWant)
Because really, if you're using Collections, you should be using
Google Collections.  share|improve this answer That does the
same thing, it just checks if it is a list first, and gets by
index if it is. It also has some code to try to fail faster on
an actual Collection (that is if the index is too large it tries
to figure that out without iterating through the whole thing and
throwing the exception at the end). –  Yishai Nov 4 '09 at 3:37
Honestly, performance-wise it might be slightly slower than
c.iterator().next() - but the