Class StreamUtils

java.lang.Object
uk.ac.ebi.utils.streams.StreamUtils

public class StreamUtils extends Object
Stream Utils
Author:
brandizi
Date:
25 Jul 2017
  • Constructor Details

    • StreamUtils

      public StreamUtils()
  • Method Details

    • tupleStream

      public static <T> Stream<T[]> tupleStream(boolean isParallel, Stream<? extends T>... streams)
      Returns a stream of tuples built from base streams. This is done by populating each tuple item with one item from the underlining streams. This is basically a wrapper of TupleSpliterator, which is passed to StreamSupport.stream(Spliterator, boolean). The built stream uses TupleSpliterator.characteristics(), which, in turn, are established on the basis of the underlining spliterators (see the method's javadoc).
      Parameters:
      isParallel - this is passed to StreamSupport.stream(Spliterator, boolean), since the underlining iterator is immutable, creating a parallel stream as result shouldn't be a problem, unless you've some strange things in the base streams.
      streams - the base streams from which the result is built.
      See Also:
      • unit tests for examples of usage.
    • tupleStream

      public static <T> Stream<T[]> tupleStream(Stream<? extends T>... streams)
      Defaults to 0 (which implies Spliterator.IMMUTABLE only) and false (i.e., non-parallel result stream).
    • sampleStream

      public static <T> Stream<T> sampleStream(Stream<T> stream, double samplingRatio)

      Returns a sampled stream, i.e., a stream where a quota of elements approximately equals to sampleRatio is returned.

      This is obtained by attaching a filter to the initial stream that returns true if randomNumber[0,1) < sampleRatio.

      sampleRatio must be between 0 and 1. 0 returns an empty stream, 1 returns the original stream.
    • sampleStream

      public static <T> Stream<T> sampleStream(Stream<T> stream, long sampleSize, long totalSize)
      A variant of sampleStream(Stream, double) that computes the sampling ratio as sampleSize/totalSize.
      Parameters:
      totalSize - must be >= 0. When 0, you'll end up having an empty stream.
      sampleSize - must be < totalSize and >= 0. When 0, you'll end up having an empty stream.