public class MoreCollectors extends Object
Collectors
. The implementation of these collectors, where
possible, attempts to emulate the behavior of the standard Collectors
factories.Modifier and Type | Method and Description |
---|---|
static <T> Collector<T,?,List<T>> |
toImmutableList()
Returns a
Collector that accumulates elements into an immutable list. |
static <T,K,U> Collector<T,?,Map<K,U>> |
toImmutableMap(Function<? super T,? extends K> keyMapper,
Function<? super T,? extends U> valueMapper)
Returns a
Collector that accumulates elements into an immutable map whose keys and values
are the result of applying the provided mapping functions to the input elements. |
static <T> Collector<T,?,Set<T>> |
toImmutableSet()
Returns a
Collector that accumulates elements into an immutable set. |
@Nonnull public static <T> Collector<T,?,List<T>> toImmutableList()
Collector
that accumulates elements into an immutable list.
The Stream
being collected may contain null
elements and, when it does, the collected
list will also contain null
elements.
T
- the type of the input elementsCollector
which collects elements into an immutable listCollectors.toList()
@Nonnull public static <T,K,U> Collector<T,?,Map<K,U>> toImmutableMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper)
Collector
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 contain null
elements, if the mapping functions handle
nulls
. However, like Collectors.toMap(Function, Function)
, this collector uses a
HashMap
to collect keys and values. Since its implementation is uses Map.merge(K, V, java.util.function.BiFunction<? super V, ? super V, ? extends V>)
, the
valueMapper
may not produce null
values, even for null
inputs, or a
NullPointerException
will be thrown. The keyMapper
, on the other hand, may
return null
keys, but may not return duplicate keys or an IllegalStateException
will be thrown.
T
- the type of the input elementsK
- the output type of the key mapping functionU
- the output type of the value mapping functionkeyMapper
- a mapping function to produce keys, which may return null
valueMapper
- a mapping function to produce values, which may not return null
Collector
which collects elements into an immutable mapCollectors.toMap(Function, Function)
@Nonnull public static <T> Collector<T,?,Set<T>> toImmutableSet()
Collector
that accumulates elements into an immutable set.
The Stream
being collected may contain null
elements and, when it does, the collected
set will also contain a null
element. Unlike Collectors.toSet()
, this collector attempts
to retain the traversal order of the stream's elements.
T
- the type of the input elementsCollector
which collects elements into an immutable setCollectors.toSet()
Copyright © 2021 Atlassian. All rights reserved.