Is there an easy way to copy an iterator into a list in Java?

Posted by Space_C0wb0y on Stack Overflow See other posts from Stack Overflow or by Space_C0wb0y
Published on 2010-05-10T11:58:44Z Indexed on 2010/05/10 12:04 UTC
Read the original article Hit count: 182

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about iterator