Package org.hortonmachine.gears.utils
Class StreamUtils
- java.lang.Object
-
- org.hortonmachine.gears.utils.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
- forEach
- forEachOrdered
- toArray
- reduce
- collect
- min
- max
- count
- anyMatch
- allMatch
- noneMatch
- findFirst
- findAny
- iterator
- anyMatch
- allMatch
- noneMatch
- findFirst
- findAny
- limit
- substream
Terminate Methods, after which the stream is consumed and no more operations can be performed on it:
Short-circuit Methods, that stop stream processing once conditions are satisfied.
- Author:
- Antonello Andrea (www.hydrologis.com)
-
-
Constructor Summary
Constructors Constructor Description StreamUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static longcountStrings(Stream<String> stream, boolean distinct, boolean ignoreCase)static <T> Stream<T>distinct(Stream<T> stream)static <T> Stream<T>findAll(Stream<T> stream, Predicate<T> predicate)static <T> TfindAny(Stream<T> stream, Predicate<T> predicate)Find an element in the stream.static <T> Stream<T>fromArray(T[] array)static <T> Stream<T>fromList(List<T> list)static <T> Stream<T>fromListParallel(List<T> list)static <T> Stream<T>fromSupplier(Supplier<T> supplier)static StringgetString(Stream<Object> stream, String separator, String prefix, String suffix)static <T,R>
voidprintLongStats(Stream<T> stream, ToLongFunction<T> summarizingFunction)static <T> List<T>toList(Stream<T> stream)static <T,K,V>
Map<K,V>toMap(Stream<T> stream, Function<T,K> keySupplier, Function<T,V> valueSupplier)Collect stream to map.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.static <T> Map<Boolean,List<T>>toMapPartition(Stream<T> stream, Predicate<T> predicate)Split a stream into a map with true and false.static <T,K>
Map<K,T>toMapWithToStringValue(Stream<T> stream, Function<T,K> keySupplier)static <T> Set<T>toSet(Stream<T> stream)static <T> TreeSet<T>toTreeSet(Stream<T> stream)
-
-
-
Method Detail
-
fromArray
public static <T> Stream<T> fromArray(T[] array)
-
countStrings
public static long countStrings(Stream<String> stream, boolean distinct, boolean ignoreCase)
-
getString
public static String getString(Stream<Object> stream, String separator, String prefix, String suffix)
-
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.
-
-