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?
views:
128answers:
1
+10
A:
Yes, and it's called Iterators.addAll(Collection, Iterator)
, unsurprisingly.
finnw
2010-02-12 18:41:26
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
2010-02-12 18:42:57
@danben, updated, thanks
finnw
2010-02-12 18:44:13
Ah, I wasn't finding the Iterators class. Guess I should have looked at the docs closer. :-)
harschware
2010-02-12 19:15:01
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
2010-02-12 20:31:58
according to the Iterables javadoc: "Except as noted, each method has a corresponding Iterator-based method in the Iterators class."
Kevin Bourrillion
2010-02-12 20:32:58