I want something like this:
public void CopyIteratorIntoList(Iterator<Foo> fooIterator) {
List<Foo> fooList = new ArrayList<Foo>();
fooList.addAll(fooIterator);
}
which should be equivalent to:
public void CopyIteratorIntoList(Iterator<Foo> fooIterator) {
List<Foo> fooList = new ArrayList<Foo>();
while(fooIterator.hasNext())
fooList.add(fooIterator.next());
}
Is there any method in the API to achieve that, or is this the only way?
No, there’s nothing like that in the Standard API.
In Java, it’s not idiomatic (and thus rather uncommon) to use
Iteratoras part of an API; they’re typically produced and immediately consumed.