Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Wednesday, April 25, 2012
Java: Get first item from a collection
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?
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.
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.
I stumble about this question sometimes when i have methods that return a collection but in some cases it is certain that there is only one element in the returned collection. Then it does not matter if the collection has an undefined order.
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.
Iterables.get(yourC, indexYouWant)
ReplyDeleteBecause really, if you're using Collections, you should be using Google Collections.
There is no such a thing as "first" item in a Collection because it is .. well simply a collection.
ReplyDeleteFrom 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.
Looks like that is the best way to do it:
ReplyDeleteString 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.
It sounds like your Collection wants to be List-like, so I'd suggest:
ReplyDeleteList<String> myList = new ArrayList<String>();
...
String first = myList.get(0);
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.
ReplyDeleteIf you know that the collection is a queue then you can cast the collection to a queue and get it easily.
ReplyDeleteThere are several structures you can use to get the order, but you will need to cast to it.
why not Arrays.asList( any string array).get(0)
ReplyDeleteI stumble about this question sometimes when i have methods that return a collection but in some cases it is certain that there is only one element in the returned collection. Then it does not matter if the collection has an undefined order.
ReplyDeleteYou could do this:
ReplyDeleteString 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.