Class BatchIterator<E>

java.lang.Object
ubic.basecode.util.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 Details

    • 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 Details

    • 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>
    • iterator

      public Iterator<Collection<E>> iterator()
      Specified by:
      iterator in interface Iterable<E>
    • next

      public Collection<E> next()
      Specified by:
      next in interface Iterator<E>
    • remove

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