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 code is much clearer and simpler to modify. –  Carl Nov 4 '09 at 4:14
I certainly agree it is cleaner, but the OP was about wasteful, but I guess since your answer was accepted that is what was desired. –  Yishai Nov 5 '09 at 20:03
For those (still) arriving here: I think jheddings' answer is probably the best "get it done" answer, though I'd prefer @DonaldRaab's (way down the page) for cases where I'm already using the GC library. My answer is really for the case where one may want to write in flexibility for later (say, if one decides that the second element is the new hotness). –  Carl May 24 '13 at 10:24
From Comments of the command: Returns the first element in iterable or defaultValue if the iterable is empty. The Iterators analog to this method is Iterators.getNext(java.util.Iterator<? extends T>, T). If no default value is desired (and the caller instead wants a NoSuchElementException to be thrown), it is recommended that iterable.iterator().next() is used instead. So, no default value needed then use iterable.iterator().next()! –  oikonomopo Dec 11 at 14:33

Looks like that is the best way to do it:

String first = strs.iterator().next();

Great question... At first, it seems like an oversight for the Collection interface.

Note that "first" won't always return the first thing you put in the collection, and may only make sense for ordered collections. Maybe that is why there isn't a get(item) call, since the order isn't necessarily perserved.

While it might seem a bit wasteful, it might not be as bad as you think. The Iterator really just contains indexing information into the collection, not a usually a copy of the entire collection. Invoking this method does instantiate the Iterator object, but that is really the only overhead (not like copying all the elements).

For example, looking at the type returned by the ArrayList<String>.iterator() method, we see that it is ArrayList::Itr. This is an internal class that just accesses the elements of the list directly, rather than copying them.

share|improve this answer
This precisely answers the question. Thanks. –  hopia Apr 4 '13 at 17:06
strange that this is not the accepted answer –  chhh Oct 29 at 22:24

There is no such a thing as "first" item in a Collection because it is .. well simply a collection.

From the Java doc's Collection.iterator() method:

There are no guarantees concerning the order in which the elements are returned...

So you can't.

If you use another interface such as List, you can do the following:

String first = strs.get(0);

But directly from a Collection this is not possible.

share|improve this answer
I don't think get(int n) is defined for Collection –  Rosarch Nov 4 '09 at 2:27
You're right, I miss that point. I have updated the answer. You can't! (unless the Collection is implemented by some underlaying class that allows provides the guarantee ) –  OscarRyz Nov 4 '09 at 2:33
get is not in the Collection interface –  Andy Gherna Nov 4 '09 at 2:33
Oscar, I think you're overstating the case. The first element of a Collection may be arbitrary in a few cases like HashSet, but it is well-defined: it's .iterator().next(). It's also stable, in every collection implementation I've ever seen. (related: note that while Set doesn't guarantee order, every single subtype of Set in the JDK except HashSet does.) –  Kevin Bourrillion Nov 4 '09 at 16:54
It might be, but consider the case when you add a new element to the collection, you don't know ( by the interface ) if that element is the first, the last, or it would be inserted in the middle. For precise results you should use another interface. Yet, probably what Rosarch needs is the 1st element no matter what. Knowing the underlaying collection may help, but it prevent you from changing it. –  OscarRyz Nov 4 '09 at 17:49

In java 8:

Optional<String> firstElement = collection.stream().findFirst();

For older versions of java, there is a getFirst method in Guava Iterables:

Iterables.getFirst(iterable, defaultValue)
share|improve this answer

In GS Collections:


This method will determine the best method of getting the first element for you based on the type of collection.

share|improve this answer

It sounds like your Collection wants to be List-like, so I'd suggest:

String first = myList.get(0);
share|improve this answer

If you know that the collection is a queue then you can cast the collection to a queue and get it easily.

There are several structures you can use to get the order, but you will need to cast to it.

share|improve this answer
I agree, if you don't wanna iterate, don't use collection. Use some other more specific interface instead. –  Adeel Ansari Nov 4 '09 at 2:41
I wonder though...let's say the actual underlying data is a SortedSet, so order makes sense, but you only have a Collection view of it (for a non-silly reason, let's say); if you cast the Collection to a List, Queue, etc and try to get/poll/etc, does disaster ensue? Likewise, if underlying structure is a List, so on, so forth. –  Carl Nov 4 '09 at 3:15
@Cal - I haven't tried it, but if you cast a collection into a very different type than it was originally you should get an error, but, I haven't tried it, so I could be wrong. –  James Black Nov 4 '09 at 3:17

You could do this:

String strz[] = strs.toArray(String[strs.size()]);
String theFirstOne = strz[0];

The javadoc for Collection gives the following caveat wrt ordering of the elements of the array:

If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.

share|improve this answer
This creates a new String array, much more expensive than creating an iterator. –  Jim Ferrans Nov 4 '09 at 2:46
Yeah, I thought about that after I posted this. Regardless of the method used, ordering depends on the underlying implementation of the Collection. "First" then becomes a relative term. However, the iterator() way of doing it is probably better in most cases. –  Andy Gherna Nov 4 '09 at 13:38
-1 because this is never better than iterator().next(). –  Kevin Bourrillion Nov 4 '09 at 16:55
I came here because this was the solution I had and found ugly. –  haansn08 Aug 8 at 22:05

Your Answer


