views:

3699

answers:

8

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?

+11  A: 

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.

OscarRyz
I don't think `get(int n)` is defined for `Collection`
Rosarch
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
get is not in the Collection interface
Andy Gherna
@arghena: Yeap. I have updated the answer to emphasize the alternative.
OscarRyz
@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
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
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
A: 

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.

Andy Gherna
This creates a new String array, much more expensive than creating an iterator.
Jim Ferrans
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
-1 because this is *never* better than iterator().next().
Kevin Bourrillion
+1  A: 

Other than collection.toArray()[0] or new ArrayList<String>(collection).get(0) I don't see any options. The Iterator is likely the most efficient solution if you'd like to know that.

BalusC
Both would be more *wasteful* ways to do it.
OscarRyz
Exactly. Both needs to iterate/map the entire collection to be created.
BalusC
+1 Although not the most efficient alternative, this may be helpful for other wanting to change the collection either to an array or a arraylist.
OscarRyz
A: 

Of course there may be a better way to access the first element if you know the implementing container class...

Rooke
+4  A: 

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.

jheddings
A: 

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.

James Black
I agree, if you don't wanna iterate, don't use collection. Use some other more specific interface instead.
Adeel Ansari
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
@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
+1  A: 

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);
Jim Ferrans
+4  A: 

Iterables.get(yourC, indexYouWant)

Because really, if you're using Collections, you should be using Google Collections.

Carl
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
Honestly, performance-wise it might be slightly slower than c.iterator().next() - but the code is much clearer and simpler to modify.
Carl
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