Package com.yahoo.stream
Class CustomCollectors
java.lang.Object
com.yahoo.stream.CustomCollectors
The purpose of this class is to fill gaps in the Java
Collectors
api
by offering convenient ways to retrieve implementations of Collector
.
For example, to get a collector that accumulates elements into a map with predictable iteration order:
Map<String, Person> idToPerson =
persons.stream().collect(toLinkedMap(Person::id, Functions.identity());
- Author:
- gjoranv
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptiontoCustomMap
(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, Supplier<M> mapSupplier) Returns aCollector
that accumulates elements into aMap
created by the given supplier.toLinkedMap
(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) Returns aCollector
that accumulates elements into aMap
that provides insertion order iteration.
-
Method Details
-
toLinkedMap
public static <T,K, Collector<T,U> ?, toLinkedMapMap<K, U>> (Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) Returns aCollector
that accumulates elements into aMap
that provides insertion order iteration. This a convenience that can be used instead of callingCollectors.toMap(Function, Function, BinaryOperator, Supplier)
. with a merger that throws upon duplicate keys.- Type Parameters:
T
- Type of the input elements.K
- Output type of the key mapping function.U
- Output type of the value mapping function.- Parameters:
keyMapper
- Mapping function to produce keys.valueMapper
- Mapping function to produce values.- Returns:
- A collector which collects elements into a map with insertion order iteration.
- Throws:
CustomCollectors.DuplicateKeyException
- If two elements map to the same key.
-
toCustomMap
public static <T,K, Collector<T,U, M extends Map<K, U>> ?, toCustomMapM> (Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, Supplier<M> mapSupplier) Returns aCollector
that accumulates elements into aMap
created by the given supplier. This a convenience that can be used instead of callingCollectors.toMap(Function, Function, BinaryOperator, Supplier)
. with a merger that throws upon duplicate keys.- Type Parameters:
T
- Type of the input elements.K
- Output type of the key mapping function.U
- Output type of the value mapping function.M
- Type of the resulting map.- Parameters:
keyMapper
- Mapping function to produce keys.valueMapper
- Mapping function to produce values.mapSupplier
- Supplier of a new map.- Returns:
- A collector which collects elements into a map created by the given supplier.
- Throws:
CustomCollectors.DuplicateKeyException
- If two elements map to the same key.
-