public class StreamUtils extends Object
| Constructor and Description |
|---|
StreamUtils() |
| Modifier and Type | Method and Description |
|---|---|
static <T> Set<T> |
append(Set<T> set,
T value)
Copy a set, add a value to it and return the new set.
|
static <T> Predicate<T> |
not(Predicate<T> predicate)
Negate the given predicate.
|
static <T> Set<T> |
setMerge(Set<T> a,
Set<T> b)
Merge two sets without modifying either.
|
static <T> BinaryOperator<T> |
throwingMerger()
Returns a merge function, suitable for use in
Map.merge() or
toMap(), which always
throws IllegalStateException. |
static <S,K,M extends Map<K,S>> |
toDictionary(Function<? super S,? extends K> keyMapper,
Supplier<M> mapSupplier)
Return a collector that creates a dictionary using the given key function and the given map supplier.
|
static <S,K> Collector<S,?,LinkedHashMap<K,S>> |
toLinkedDictionary(Function<? super S,? extends K> keyMapper)
Return a collector that creates a LinkedHashMap dictionary using the given key function.
|
static <S,K,V> Collector<S,?,LinkedHashMap<K,V>> |
toLinkedMap(Function<? super S,? extends K> keyMapper,
Function<? super S,? extends V> valueMapper)
Return a collector that creates a LinkedHashMap using the given key and value functions.
|
static <S,K,V,M extends Map<K,V>> |
toMap(Function<? super S,? extends K> keyMapper,
Function<? super S,? extends V> valueMapper,
Supplier<M> mapSupplier)
Return a collector that creates a
Map using the given key and value functions. |
static <T> Set<T> |
toUnmodifiableSet(Stream<T> values)
Collect a stream into an unmodifiable set preserving first-in ordering.
|
static <T,R> Function<T,R> |
uncheckedCast(Class<R> targetType)
Return a function that can be used to do unchecked casts without generating a compiler warning.
|
public static <T> BinaryOperator<T> throwingMerger()
Map.merge() or
toMap(), which always
throws IllegalStateException. This can be used to enforce the
assumption that the elements being collected are distinct.T - the type of input arguments to the merge functionIllegalStateExceptionCollectors.throwingMerger()public static <S,K,V> Collector<S,?,LinkedHashMap<K,V>> toLinkedMap(Function<? super S,? extends K> keyMapper, Function<? super S,? extends V> valueMapper)
S - Type of the objects being collectedK - Type of the keysV - Type of the valueskeyMapper - Mapping function for the keyvalueMapper - Mapping function for the valueIllegalStateException - if multiple values are associated with the same keyCollectors.toMap(Function, Function, BinaryOperator, java.util.function.Supplier)public static <S,K,V,M extends Map<K,V>> Collector<S,?,M> toMap(Function<? super S,? extends K> keyMapper, Function<? super S,? extends V> valueMapper, Supplier<M> mapSupplier)
Map using the given key and value functions.
This collector assumes the elements being collected are distinct.
The concrete type of the Map being created is given by a Supplier.
S - Type of the objects being collectedK - Type of the keysV - Type of the valuesM - The type of Map being collected intokeyMapper - Mapping function for the keyvalueMapper - Mapping function for the valuemapSupplier - A Supplier which provides a new, empty Map into
which the results will be insertedMap using the given key and value functionsIllegalStateException - if multiple values are associated with the same keyCollectors.toMap(Function, Function, BinaryOperator, java.util.function.Supplier)public static <S,K> Collector<S,?,LinkedHashMap<K,S>> toLinkedDictionary(Function<? super S,? extends K> keyMapper)
S - Type of the objects being collected and the values of the dictionary / mapK - Type of the keyskeyMapper - Mapping function for the keyIllegalStateException - if multiple values are associated with the same keyCollectors.toMap(Function, Function, BinaryOperator, java.util.function.Supplier)public static <S,K,M extends Map<K,S>> Collector<S,?,M> toDictionary(Function<? super S,? extends K> keyMapper, Supplier<M> mapSupplier)
S - Type of the objects being collected and the values of the dictionary / mapK - Type of the keysM - The type of Map being collected intokeyMapper - Mapping function for the keymapSupplier - A Supplier which provides a new, empty Map into
which the results will be insertedCollector that creates a dictionary using the given
key function and the given map supplierIllegalStateException - if multiple values are associated with the same keyCollectors.toMap(Function, Function, BinaryOperator, java.util.function.Supplier)public static <T> Set<T> toUnmodifiableSet(Stream<T> values)
T - The type of the value streamvalues - The stream of values to be collectedpublic static <T> Predicate<T> not(Predicate<T> predicate)
T - Type of the predicate being negatedpredicate - Predicate to negatepublic static <T,R> Function<T,R> uncheckedCast(Class<R> targetType)
Essentially the same as annotating the cast with @SuppressWarnings("unchecked"), which you can't do
inside a stream.
T - Operand type (type of function parameter)R - Cast typetargetType - Class to cast the operand (function parameter) to. Used only to set the generic typepublic static <T> Set<T> append(Set<T> set, T value)
T - The type of the setset - The original set being copied.value - The value being appended to the set.public static <T> Set<T> setMerge(Set<T> a, Set<T> b)
BinaryOperator for use in stream reductions.T - The type of the underlying setsa - One input setb - Another input setCopyright © 2016–2018 Yahoo! Inc.. All rights reserved.