Class Partition<T>

All Implemented Interfaces:
Iterable<List<T>>, Collection<List<T>>, List<List<T>>, SequencedCollection<List<T>>

public final class Partition<T> extends AbstractList<List<T>>
Separates a given list into smaller chunks (partitions).

Useful if you want to bulk-process large amounts of data, but want to interrupt for a different task every X items.

Usage example:


   Partition<String> lines = Partition.of(myHugeTextFileData, 500);
   for (List<String> linesChunk : lines) { // gives you a list with up to 500 items
     for (String line : linesChunk) {
       // processing
     }
     updateUI(linesChunk.size());
   }
 
 
  • Method Details

    • of

      public static <T> Partition<T> of(List<T> list, int chunkSize)
      Creates a partitioned view of a given list with a given chunk size.
      Type Parameters:
      T - any
      Parameters:
      list - the source data
      chunkSize - how many items are in each partition.
      Returns:
      a partitioned view of the source data.
    • get

      public List<T> get(int index)
      Returns a chunk of the partitioned content with the given index.
      Specified by:
      get in interface List<T>
      Specified by:
      get in class AbstractList<List<T>>
      Parameters:
      index - the chunk index of the underlying data.
      Returns:
      a list of all elements in the requested chunk.
    • size

      public int size()
      Specified by:
      size in interface Collection<T>
      Specified by:
      size in interface List<T>
      Specified by:
      size in class AbstractCollection<List<T>>