Java Iterator fillCollection(final Iterator iterator, final Collection collection)

Here you can find the source of fillCollection(final Iterator iterator, final Collection collection)

Description

Drain an iterator into a collection.

License

Apache License

Parameter

Parameter Description
iterator the iterator to drain
collection the collection to fill
T the object type of the iterator

Declaration

public static <T> void fillCollection(final Iterator<T> iterator,
        final Collection<T> collection) 

Method Source Code

//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) {
        }
    }
}

Related

  1. endOfData(Iterator inputIter, Iterator resultIter)
  2. equalsIterator(Iterator it, Iterator it2)
  3. fill(final Iterator iterator, final S collection)
  4. fillArray(Iterator ii, Object[] fillMe, boolean null_terminate)
  5. fillCollection(final Iterator iterator, final Collection collection)
  6. firstOrDefault(Iterator iterator)
  7. fmtSeq(Iterator it, String divider)
  8. fromIterator(Iterator iterator)
  9. fromIterator(Iterator iterator)