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?

- 119,074
- 188
- 476
- 699
-
1Of course there may be a better way to access the first element if you know the implementing container class... – Rooke Nov 04 '09 at 02:32
-
Generalization for any index: http://stackoverflow.com/questions/1047957/best-way-to-get-value-from-collection-by-index – Ciro Santilli OurBigBook.com Mar 23 '15 at 16:37
-
1It sounds like you need Queue.peek() – Johannes Oct 28 '15 at 10:21
14 Answers
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 preserved.
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.
Just be sure you check the return of iterator()
since it may be empty or null
depending on the implementation.

- 26,717
- 8
- 52
- 65
-
5It is important to note that this "trick" only works when the collection actually has contents. If it's empty the iterator might return an error, in which one has to check the collection size beforehand. – spaceemotion Nov 06 '15 at 17:05
-
37This should be the correct answer. I do not understand why the answer is always "use another library!" . – Kuzeko May 18 '16 at 14:44
-
How about the second element of the collection? Why first->next() doesn't work? What should I do? Thanks! – pb772 May 18 '17 at 07:42
-
not safe enough, it's not guaranteed that collection will always point to the 1st element. – Next Developer Jan 16 '19 at 19:35
-
This should be the accepted answer: simple and no additional requirements. – Marco Sulla Aug 24 '21 at 12:44
Iterables.get(yourC, indexYouWant)
Because really, if you're using Collections, you should be using Google Collections.
-
7That 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 04 '09 at 03:37
-
1Honestly, performance-wise it might be slightly slower than c.iterator().next() - but the code is much clearer and simpler to modify. – Carl Nov 04 '09 at 04:14
-
3I 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 05 '09 at 20:03
-
12For 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 '14 at 14:33
-
4Sometimes you're just using code that uses Collections, so there's not much to do. – erickrf Aug 09 '15 at 03:08
-
-
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)

- 6,315
- 2
- 41
- 54

- 110,878
- 29
- 149
- 111
-
10The java 8 solution is especially useful, because it handles the case where the collection is empty gracefully. – SpaceTrucker Feb 16 '16 at 08:14
-
4Not good. You add overhead of stream() to get a get(0) just because you are lazy to write 4 lines of code. if(!CollectionUtils.isEmpty(productList)){ return Optional.of(productList.get(0)); } return Optional.empty(); – R.A Mar 22 '17 at 07:18
-
I don't have `getFirst` method available. There are `get` and `getLast` methods – user1209216 Aug 03 '17 at 05:41
-
9@R.S and what happens if you can't call productList.get(0), as it's a Collection..? (As per OP question) – Denham Coote Nov 16 '17 at 14:17
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.

- 196,001
- 113
- 385
- 569
-
14
-
2You'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 04 '09 at 02:33
-
-
@arghena: Yeap. I have updated the answer to emphasize the alternative. – OscarRyz Nov 04 '09 at 02:40
-
@Oscar: sorry, I must've posted before I saw your edit (I thought my comment was first in fact). My browser must've been slow :P – Andy Gherna Nov 04 '09 at 13:39
-
21Oscar, 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 04 '09 at 16:54
-
3It 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 04 '09 at 17:49
-
Good point. But, sometimes it maybe necessary to pull any random one. Even if the "first one" is not always the same, the advantage to knowing its the first one is you'll know (or at least hope, not a java expert) that the entire collection has been iterated through, wasting resources and time. – Doug Molineux Mar 28 '11 at 19:57
-
-1: The `n`th item of any `Iterable` is easily well-defined. When working with a `List`, you have additional guarantees that the operation is stable (and can often be performed in O(1) time), but it doesn't change the definition of the `n`th item of an `Iterable`. – Sam Harwell Sep 30 '11 at 14:19
-
also, if you *know* a collection will only have one element in it, then getting the first one makes sense ;-) – Brad Parks Mar 22 '12 at 18:48
It sounds like your Collection wants to be List-like, so I'd suggest:
List<String> myList = new ArrayList<String>();
...
String first = myList.get(0);

- 30,582
- 12
- 56
- 83
If you are using Apache Commons Collections 4 there is an IterableUtils.first
method. It contains an optimization in the case of List
s and is neat to use. It's very similar to the Guava method. The code would look like
String firstStr = IterableUtils.first(strs);

- 1,816
- 1
- 20
- 25
Functional way:
public static <T> Optional<T> findFirst(List<T> result) {
return Optional.ofNullable(result)
.map(List::stream)
.flatMap(Stream::findFirst);
}
above code snippet preserve from NullPointerException and IndexOutOfBoundsException

- 87
- 4
-
1Your choice of `List
` doesn't satisfy the condition that it should work for a `Collection – Scratte Mar 30 '20 at 11:08`, but of course that can be fixed using `Collection `, with the additional change: `.map(Collection::stream)`.
In Java 8 you have some many operators to use, for instance limit
/**
* Operator that limit the total number of items emitted through the pipeline
* Shall print
* [1]
* @throws InterruptedException
*/
@Test
public void limitStream() throws InterruptedException {
List<Integer> list = Arrays.asList(1, 2, 3, 1, 4, 2, 3)
.stream()
.limit(1)
.collect(toList());
System.out.println(list);
}

- 12,873
- 23
- 91
- 153
-
2@Vitalii Fedorenko's answer http://stackoverflow.com/a/18165855/1562662 is better. – Chacko Mar 09 '17 at 07:26
Guava provides an onlyElement
Collector
, but only use it if you expect the collection to have exactly one element.
Collection<String> stringCollection = ...;
String string = collection.stream().collect(MoreCollectors.onlyElement())
If you are unsure of how many elements there are, use findFirst
.
Optional<String> optionalString = collection.stream().findFirst();

- 8,391
- 5
- 34
- 53
You can do a casting. For example, if exists one method with this definition, and you know that this method is returning a List:
Collection<String> getStrings();
And after invoke it, you need the first element, you can do it like this:
List<String> listString = (List) getStrings();
String firstElement = (listString.isEmpty() ? null : listString.get(0));

- 589
- 5
- 15
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.

- 41,583
- 10
- 86
- 166
-
I agree, if you don't wanna iterate, don't use collection. Use some other more specific interface instead. – Adeel Ansari Nov 04 '09 at 02:41
-
1I 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 04 '09 at 03: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 04 '09 at 03:17
It totally depends upon which implementation you have used, whether arraylist linkedlist, or other implementations of set.
if it is set then you can directly get the first element , their can be trick loop over the collection , create a variable of value 1 and get value when flag value is 1 after that break that loop.
if it is list's implementation then it is easy by defining index number.

- 1,194
- 2
- 13
- 29
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.

- 2,135
- 18
- 22
-
2This creates a new String array, much more expensive than creating an iterator. – Jim Ferrans Nov 04 '09 at 02: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 04 '09 at 13:38
-
2