Here you can find the source of toCollection(final Iterator
public static <E> void toCollection(final Iterator<E> iterator, final Collection<? super E> c)
//package com.java2s; //License from project: Open Source License import java.util.Collection; import java.util.Iterator; public class Main { public static <E> void toCollection(final Iterator<E> iterator, final Collection<? super E> c) { while (iterator.hasNext()) { c.add(iterator.next());/* w w w . ja v a 2 s . co m*/ } } public static <E> void toCollection(final Iterator<? extends E> iterator, final Collection<? super E> c, final int limit) { for (int i = 0; (i < limit) && iterator.hasNext(); i++) { c.add(iterator.next()); } } }