views:

128

answers:

1

Google Collections has an Iterables utility class for taking in a collection and iterable and putting all the elements from iterable into the collection, called addAll. Is there something similar when all you have is an iterator?

+10  A: 

Yes, and it's called Iterators.addAll(Collection, Iterator), unsurprisingly.

finnw
Beat me to it; here's the link from my answer: http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Iterators.html
danben
@danben, updated, thanks
finnw
Ah, I wasn't finding the Iterators class. Guess I should have looked at the docs closer. :-)
harschware
Also, if your purpose is to fill up a collection that you won't change later, you can use ImmutableSet.copyOf(Iterator), etc.
Kevin Bourrillion
according to the Iterables javadoc: "Except as noted, each method has a corresponding Iterator-based method in the Iterators class."
Kevin Bourrillion