Is there an easy way to copy an iterator into a list in Java?
- by Space_C0wb0y
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?