Class StreamsUtil


public class StreamsUtil extends Object
  • Constructor Details

    • StreamsUtil

      public StreamsUtil()
  • Method Details

    • closing

      public static <T> Stream<T> closing(Stream<T> stream)
      Returns the original stream that is closed on terminating operation. It is used, for example, for closing hibernate provided streams since it is required by hibernate documentation.
      stream - the stream which is expected to be closed on termination
      stream that will be closed on terminating operation
    • throwIfEmpty

      public static <T> Stream<T> throwIfEmpty(Stream<T> stream, RuntimeException ex)
      Returns the original stream if the stream is not empty. Otherwise throws the provided exception.
      stream - Stream to be examined.
      ex - Exception to be thrown if the stream is empty.
    • paginatedStream

      public static <T> Stream<T> paginatedStream(Stream<T> originalStream, Integer first, Integer max)
      Returns the original stream that is limited with skip and limit functions based on values of first and max parameters.
      Type Parameters:
      T - Type of items in the stream
      originalStream - Stream to be limited.
      first - Index of first item to be returned by the stream. Ignored if negative, zero null.
      max - Maximum number of items to be returned by the stream. Ignored if negative or null.
    • distinctByKey

      public static <T> Predicate<T> distinctByKey(Function<? super T,?> keyExtractor)
      distinctByKey is not supposed to be used with parallel streams To make this method synchronized use ConcurrentHashMap<Object, Boolean> instead of HashSet
    • chunkedStream

      public static <T> Stream<Collection<T>> chunkedStream(Stream<T> originalStream, int chunkSize)
      A Java stream utility that splits a stream into chunks of a fixed size. Last chunk in the stream might be smaller than the desired size. Ordering guarantees depend on underlying stream.
      Type Parameters:
      T - The type of the stream
      originalStream - The original stream
      chunkSize - The chunk size
      The stream in chunks