Class StreamUtils


  • public class StreamUtils
    extends Object
    Utilities for Streams. This more a streams j8 documentation then something to really use.

    Use .peek(e->sysout) to debug without interference on the stream's items

    Use primitive streams were possible: IntStream, LongStream, DoubleStream.

    Intermediate Methods, which produce again streams:

    • map
    • filter
    • distinct
    • sorted
    • peek
    • limit
    • substream
    • parallel
    • sequential
    • unordered
      • Terminate Methods, after which the stream is consumed and no more operations can be performed on it:

      • forEach
      • forEachOrdered
      • toArray
      • reduce
      • collect
      • min
      • max
      • count
      • anyMatch
      • allMatch
      • noneMatch
      • findFirst
      • findAny
      • iterator
      • Short-circuit Methods, that stop stream processing once conditions are satisfied.

      • anyMatch
      • allMatch
      • noneMatch
      • findFirst
      • findAny
      • limit
      • substream
    Author:
    Antonello Andrea (www.hydrologis.com)
    • Constructor Detail

      • StreamUtils

        public StreamUtils()
    • Method Detail

      • fromArray

        public static <T> Stream<T> fromArray​(T[] array)
      • fromList

        public static <T> Stream<T> fromList​(List<T> list)
      • fromListParallel

        public static <T> Stream<T> fromListParallel​(List<T> list)
      • fromSupplier

        public static <T> Stream<T> fromSupplier​(Supplier<T> supplier)
      • distinct

        public static <T> Stream<T> distinct​(Stream<T> stream)
      • countStrings

        public static long countStrings​(Stream<String> stream,
                                        boolean distinct,
                                        boolean ignoreCase)
      • toList

        public static <T> List<T> toList​(Stream<T> stream)
      • toSet

        public static <T> Set<T> toSet​(Stream<T> stream)
      • toTreeSet

        public static <T> TreeSet<T> toTreeSet​(Stream<T> stream)
      • toMap

        public static <T,​K,​V> Map<K,​V> toMap​(Stream<T> stream,
                                                               Function<T,​K> keySupplier,
                                                               Function<T,​V> valueSupplier)
        Collect stream to map.
        Parameters:
        stream - the stream to convert.
        keySupplier - the supplier for the key, ex. Object::getId()
        valueSupplier - the value supplier, ex. Object::getValue()
        Returns:
        the map.
      • toMapWithToStringValue

        public static <T,​K> Map<K,​T> toMapWithToStringValue​(Stream<T> stream,
                                                                        Function<T,​K> keySupplier)
      • toMapGroupBy

        public static <T,​K,​R> Map<R,​List<T>> toMapGroupBy​(Stream<T> stream,
                                                                            Function<T,​R> groupingFunction)
        Collect a map by grouping based on the object's field.
        Parameters:
        stream - the stream to collect.
        groupingFunction - the function supplying the grouping field, ex. Object::getField()
        Returns:
        the map of lists.
      • toMapPartition

        public static <T> Map<Boolean,​List<T>> toMapPartition​(Stream<T> stream,
                                                                    Predicate<T> predicate)
        Split a stream into a map with true and false.
        Parameters:
        stream - the stream to collect.
        predicate - the predicate to use to define true or false, ex. e->e.getValue().equals("test")
        Returns:
        the map of lists.
      • printLongStats

        public static <T,​R> void printLongStats​(Stream<T> stream,
                                                      ToLongFunction<T> summarizingFunction)
      • findAny

        public static <T> T findAny​(Stream<T> stream,
                                    Predicate<T> predicate)
        Find an element in the stream.
        Parameters:
        stream - the stream to check. This should be parallel.
        predicate - the predicate to use.
        Returns:
        the element or null.