Class MoreCollectors
Collectors
. The implementation of these collectors, where
possible, attempts to emulate the behavior of the standard Collectors
factories.-
Method Summary
Modifier and TypeMethodDescriptionReturns aCollector
that accumulates elements into an immutable list.toImmutableMap
(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) Returns aCollector
that accumulates elements into an immutable map whose keys and values are the result of applying the provided mapping functions to the input elements.Returns aCollector
that accumulates elements into an immutable set.
-
Method Details
-
toImmutableList
Returns aCollector
that accumulates elements into an immutable list.The
Stream
being collected may containnull
elements and, when it does, the collected list will also containnull
elements.- Type Parameters:
T
- the type of the input elements- Returns:
- a
Collector
which collects elements into an immutable list - See Also:
-
toImmutableMap
@Nonnull public static <T,K, Collector<T,U> ?, toImmutableMapMap<K, U>> (Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) Returns aCollector
that accumulates elements into an immutable map whose keys and values are the result of applying the provided mapping functions to the input elements.The
Stream
being collected may containnull
elements, if the mapping functions handlenulls
. However, likeCollectors.toMap(Function, Function)
, this collector uses aHashMap
to collect keys and values. Since its implementation is usesMap.merge(K, V, java.util.function.BiFunction<? super V, ? super V, ? extends V>)
, thevalueMapper
may not producenull
values, even fornull
inputs, or aNullPointerException
will be thrown. ThekeyMapper
, on the other hand, may returnnull
keys, but may not return duplicate keys or anIllegalStateException
will be thrown.- Type Parameters:
T
- the type of the input elementsK
- the output type of the key mapping functionU
- the output type of the value mapping function- Parameters:
keyMapper
- a mapping function to produce keys, which may returnnull
valueMapper
- a mapping function to produce values, which may not returnnull
- Returns:
- a
Collector
which collects elements into an immutable map - Since:
- 4.8
- See Also:
-
toImmutableSet
Returns aCollector
that accumulates elements into an immutable set.The
Stream
being collected may containnull
elements and, when it does, the collected set will also contain anull
element. UnlikeCollectors.toSet()
, this collector attempts to retain the traversal order of the stream's elements.- Type Parameters:
T
- the type of the input elements- Returns:
- a
Collector
which collects elements into an immutable set - See Also:
-