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 aCollectorthat accumulates elements into an immutable list.toImmutableMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) Returns aCollectorthat accumulates elements into an immutable map whose keys and values are the result of applying the provided mapping functions to the input elements.Returns aCollectorthat accumulates elements into an immutable set.
-
Method Details
-
toImmutableList
Returns aCollectorthat accumulates elements into an immutable list.The
Streambeing collected may containnullelements and, when it does, the collected list will also containnullelements.- Type Parameters:
T- the type of the input elements- Returns:
- a
Collectorwhich 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 aCollectorthat accumulates elements into an immutable map whose keys and values are the result of applying the provided mapping functions to the input elements.The
Streambeing collected may containnullelements, if the mapping functions handlenulls. However, likeCollectors.toMap(Function, Function), this collector uses aHashMapto collect keys and values. Since its implementation is usesMap.merge(K, V, java.util.function.BiFunction<? super V, ? super V, ? extends V>), thevalueMappermay not producenullvalues, even fornullinputs, or aNullPointerExceptionwill be thrown. ThekeyMapper, on the other hand, may returnnullkeys, but may not return duplicate keys or anIllegalStateExceptionwill 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 returnnullvalueMapper- a mapping function to produce values, which may not returnnull- Returns:
- a
Collectorwhich collects elements into an immutable map - Since:
- 4.8
- See Also:
-
toImmutableSet
Returns aCollectorthat accumulates elements into an immutable set.The
Streambeing collected may containnullelements and, when it does, the collected set will also contain anullelement. 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
Collectorwhich collects elements into an immutable set - See Also:
-