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
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionReturns aCollectorthat returns a singleton, or throws anIllegalArgumentExceptionif there are more than one item.toCustomMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, Supplier<M> mapSupplier) Returns aCollectorthat accumulates elements into aMapcreated by the given supplier.toLinkedMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) Returns aCollectorthat accumulates elements into aMapthat 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 aCollectorthat accumulates elements into aMapthat 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 aCollectorthat accumulates elements into aMapcreated 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.
-
singleton
Returns aCollectorthat returns a singleton, or throws anIllegalArgumentExceptionif there are more than one item.- Type Parameters:
T- Type of the input elements.- Returns:
- A collector returning an optional element
- Throws:
IllegalArgumentException- if there are more than one element
-