Class BatchIterator<E>

  • All Implemented Interfaces:
    Iterable<Collection<E>>, Iterator<Collection<E>>

    public class BatchIterator<E>
    extends Object
    implements Iterable<Collection<E>>, Iterator<Collection<E>>

    Example usage:

    Collection items; int batchSize = 1000; for ( Collection batch : BatchIterator.batches( items, batchSize ) ) { }

    or

    Collection items; int batchSize = 1000; BatchIterator iterator = new BatchIterator( items, batchSize ); while ( iterator.hasNext() ) { Collection batch = iterator.next(); }
    Author:
    luke
    • Constructor Detail

      • BatchIterator

        public BatchIterator​(Collection<E> collection,
                             int batchSize)
        Returns a BatchIterator over the specified collection.
        Parameters:
        collection - the collection over which to iterate
        batchSize - the maximum size of each batch returned
    • Method Detail

      • batches

        public static <E> BatchIterator<E> batches​(Collection<E> collection,
                                                   int batchSize)
        Returns a BatchIterator over the specified collection. This is a convenience method to simplify the code need to loop over an existing collection.
        Parameters:
        collection - the collection over which to iterate
        batchSize - the maximum size of each batch returned
        Returns:
        a BatchIterator over the specified collection
      • hasNext

        public boolean hasNext()
        Specified by:
        hasNext in interface Iterator<E>
      • remove

        public void remove()
        Specified by:
        remove in interface Iterator<E>