Here you can find the source of fillCollection(final Iterator
Parameter | Description |
---|---|
iterator | the iterator to drain |
collection | the collection to fill |
T | the object type of the iterator |
public static <T> void fillCollection(final Iterator<T> iterator, final Collection<T> collection)
//package com.java2s; //License from project: Apache License import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; public class Main { /**//from www. j ava 2 s. c o m * Drain an iterator into a collection. Useful for storing the results of a Pipe into a collection. * Note that the try/catch model is not "acceptable Java," but is more efficient given the architecture of AbstractPipe. * * @param iterator the iterator to drain * @param collection the collection to fill * @param <T> the object type of the iterator */ public static <T> void fillCollection(final Iterator<T> iterator, final Collection<T> collection) { try { while (true) { collection.add(iterator.next()); } } catch (final NoSuchElementException e) { } } /** * Drain an iterator into a collection. Useful for storing the results of a Pipe into a collection. * Note that the try/catch model is not "acceptable Java," but is more efficient given the architecture of AbstractPipe. * * @param iterator the iterator to drain * @param collection the collection to fill * @param number the number of objects to fill into the collection (ordered by iterator) * @param <T> the object type of the iterator */ public static <T> void fillCollection(final Iterator<T> iterator, final Collection<T> collection, final int number) { try { for (int i = 0; i < number; i++) { collection.add(iterator.next()); } } catch (final NoSuchElementException e) { } } }