Class MoreCollectors
Collector
in addition to relevant utilities in JDK and Guava.- Since:
- 5.2
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T,
R> Collector <T, ?, R> allMax
(Comparator<? super T> comparator, Collector<? super T, ?, R> downstream) Returns aCollector
that collects all of the greatest (relative to the specifiedComparator
) input elements, in encounter order, using thedownstream
collector.static <T,
R> Collector <T, ?, R> allMin
(Comparator<? super T> comparator, Collector<? super T, ?, R> downstream) Returns aCollector
that collects all of the least (relative to the specifiedComparator
) input elements, in encounter order, using thedownstream
collector.static <T,
A, B, R>
Collector<T, ?, R> collectingAndThen
(Collector<T, ?, ? extends Both<? extends A, ? extends B>> upstream, BiFunction<? super A, ? super B, ? extends R> finisher) static <T,
R> FixedSizeCollector <T, ?, R> Returns a collector that collects the only three elements from the input and transforms them using themapper
function.static <T,
R> FixedSizeCollector <T, ?, R> Returns a collector that collects the only four elements from the input and transforms them using themapper
function.static <T,
R> FixedSizeCollector <T, ?, R> Returns a collector that collects the only five elements from the input and transforms them using themapper
function.static <T,
R> FixedSizeCollector <T, ?, R> Returns a collector that collects the only six elements from the input and transforms them using themapper
function.static <T,
R> FixedSizeCollector <T, ?, R> Returns a collector that collects the only seven elements from the input and transforms them using themapper
function.static <T,
R> FixedSizeCollector <T, ?, R> Returns a collector that collects the only eight elements from the input and transforms them using themapper
function.static <T,
R> FixedSizeCollector <T, ?, R> combining
(BiFunction<? super T, ? super T, ? extends R> mapper) Returns a collector that collects the only two elements from the input and transforms them using themapper
function.static <T,
K, V, R>
Collector<T, ?, R> flatMapping
(Function<? super T, ? extends BiStream<? extends K, ? extends V>> flattener, BiCollector<K, V, R> downstream) Similar but slightly different thanCollectors.flatMapping(java.util.function.Function<? super T, ? extends java.util.stream.Stream<? extends U>>, java.util.stream.Collector<? super U, A, R>)
, returns aCollector
that first flattens the input stream of pairs (as opposed to single elements) and then collects the flattened pairs with thedownstream
BiCollector.flatteningMaps
(BiCollector<K, V, R> downstream) Returns aCollector
that flattens the inputMap
entries and collects them using thedownstream
BiCollector.static <F,
T, T1, R>
Collector<F, ?, R> mapping
(Function<? super F, ? extends T> inputMapper, Collector<T, ?, T1> collector, Function<? super T1, ? extends R> outputMapper) Returns a collector that usesinputMapper
function to transform each input, and finally usesoutputMapper
to transform the output of the givencollector
.static <T,
A, B, R>
Collector<T, ?, R> mapping
(Function<? super T, ? extends Both<? extends A, ? extends B>> mapper, BiCollector<A, B, R> downstream) Analogous toCollectors.mapping()
, applies a mapping function to each input element before accumulation, except that themapper
function returns a pair of elements, which are then accumulated by a BiCollector.static <T> Collector
<T, ?, BiOptional<T, T>> minMax
(Comparator<? super T> comparator) Returns a collector that collects the minimum and maximum elements from the input elements.static <T,
R> FixedSizeCollector <T, ?, R> onlyElement
(Function<? super T, ? extends R> mapper) Returns collector that collects the single element from the input.static <T,
R> FixedSizeCollector <T, ?, R> onlyElements
(MapFrom3<? super T, ? extends R> mapper) Same ascombining(MapFrom3)
.static <T,
R> FixedSizeCollector <T, ?, R> onlyElements
(MapFrom4<? super T, ? extends R> mapper) Same ascombining(MapFrom4)
.static <T,
R> FixedSizeCollector <T, ?, R> onlyElements
(MapFrom5<? super T, ? extends R> mapper) Same ascombining(MapFrom5)
.static <T,
R> FixedSizeCollector <T, ?, R> onlyElements
(MapFrom6<? super T, ? extends R> mapper) Same ascombining(MapFrom6)
.static <T,
R> FixedSizeCollector <T, ?, R> onlyElements
(MapFrom7<? super T, ? extends R> mapper) Same ascombining(MapFrom7)
.static <T,
R> FixedSizeCollector <T, ?, R> onlyElements
(MapFrom8<? super T, ? extends R> mapper) Same ascombining(MapFrom7)
.static <T,
R> FixedSizeCollector <T, ?, R> onlyElements
(BiFunction<? super T, ? super T, ? extends R> mapper) Same ascombining(BiFunction)
.partitioningBy
(Predicate<? super E> predicate, Collector<E, ?, ? extends R> downstream) Returns a collector that partitions the incoming elements into two groups: elements that matchpredicate
, and those that don't.partitioningBy
(Predicate<? super E> predicate, Collector<E, A1, ? extends T> downstreamIfTrue, Collector<E, A2, ? extends F> downstreamIfFalse) Returns a collector that partitions the incoming elements into two groups: elements that matchpredicate
, and those that don't, and usedownstreamIfTrue
anddownstreamIfFalse
respectively to collect the elements.static <T,
R> Collector <T, ?, R> switching
(FixedSizeCollector<T, ?, R> firstCase, FixedSizeCollector<T, ?, R>... moreCases) Returns aCollector
that will collect the input elements using the first of[firstCase, moreCases...]
that matches the input elements.toListAndThen
(Consumer<? super List<T>> arranger) Returns a collector that collects input elements into a list, which is then arranged by thearranger
function before being wrapped as immutable list result.toMap
(Function<? super T, ? extends K> keyFunction, Function<? super T, ? extends V> valueFunction, Supplier<? extends M> mapSupplier) Returns aCollector
that extracts the keys and values through the givenkeyFunction
andvalueFunction
respectively, and then collects them into a mutableMap
created bymapSupplier
.
-
Method Details
-
mapping
public static <F,T, Collector<F,T1, R> ?, mappingR> (Function<? super F, ? extends T> inputMapper, Collector<T, ?, T1> collector, Function<? super T1, ? extends R> outputMapper) Returns a collector that usesinputMapper
function to transform each input, and finally usesoutputMapper
to transform the output of the givencollector
.- Since:
- 6.0
-
mapping
public static <T,A, Collector<T,B, R> ?, mappingR> (Function<? super T, ? extends Both<? extends A, ? extends B>> mapper, BiCollector<A, B, R> downstream) Analogous toCollectors.mapping()
, applies a mapping function to each input element before accumulation, except that themapper
function returns a pair of elements, which are then accumulated by a BiCollector.For example, you can parse key-value pairs in the form of "k1=v1,k2=v2" with:
Substring.first(',') .repeatedly() .split("k1=v2,k2=v2") .collect( mapping( s -> first('=').split(s).orElseThrow(...), toImmutableSetMultimap()));
-
flatMapping
public static <T,K, Collector<T,V, R> ?, flatMappingR> (Function<? super T, ? extends BiStream<? extends K, ? extends V>> flattener, BiCollector<K, V, R> downstream) Similar but slightly different thanCollectors.flatMapping(java.util.function.Function<? super T, ? extends java.util.stream.Stream<? extends U>>, java.util.stream.Collector<? super U, A, R>)
, returns aCollector
that first flattens the input stream of pairs (as opposed to single elements) and then collects the flattened pairs with thedownstream
BiCollector. -
flatteningMaps
Returns aCollector
that flattens the inputMap
entries and collects them using thedownstream
BiCollector.For example, you can flatten a list of multimaps:
ImmutableMap<EmployeeId, Task> billableTaskAssignments = projects.stream() .map(Project::getTaskAssignments) .collect(flatteningMaps(ImmutableMap::toImmutableMap)));
-
toMap
public static <T,K, Collector<T,V, M extends Map<K, V>> ?, toMapM> (Function<? super T, ? extends K> keyFunction, Function<? super T, ? extends V> valueFunction, Supplier<? extends M> mapSupplier) Returns aCollector
that extracts the keys and values through the givenkeyFunction
andvalueFunction
respectively, and then collects them into a mutableMap
created bymapSupplier
.Duplicate keys will cause
IllegalArgumentException
to be thrown, with the offending key reported in the error message.Null keys and values are discouraged but supported as long as the result
Map
supports them. Thus this method can be used as a workaround of the toMap(Supplier) JDK bug that fails to support null values.- Since:
- 5.9
-
toListAndThen
Returns a collector that collects input elements into a list, which is then arranged by thearranger
function before being wrapped as immutable list result. List elements are not allowed to be null.Example usages:
stream.collect(toListAndThen(Collections::reverse))
to collect to reverse order.stream.collect(toListAndThen(Collections::shuffle))
to collect and shuffle.stream.collect(toListAndThen(Collections::sort))
to collect and sort.
-
collectingAndThen
public static <T,A, Collector<T,B, R> ?, collectingAndThenR> (Collector<T, ?, ? extends Both<? extends A, ? extends B>> upstream, BiFunction<? super A, ? super B, ? extends R> finisher) Returns aCollector
that maps the result ofupstream
collector using thefinisher
BiFunction. Useful when combined with collectors likepartitioningBy(java.util.function.Predicate<? super E>, java.util.stream.Collector<E, ?, ? extends R>)
.For example:
collectingAndThen(partitioningBy(Person::isGood), (good, evil) -> ...)
- Since:
- 8.1
-
combining
public static <T,R> FixedSizeCollector<T,?, combiningR> (BiFunction<? super T, ? super T, ? extends R> mapper) Returns a collector that collects the only two elements from the input and transforms them using themapper
function. If there are fewer or more elements in the input, IllegalArgumentExceptioin is thrown.To handle the
size() != 2
case, consider to use theMoreCollections.findOnlyElements()
method, which returnsOptional
.- Since:
- 6.6
-
combining
Returns a collector that collects the only three elements from the input and transforms them using themapper
function. If there are fewer or more elements in the input, IllegalArgumentExceptioin is thrown.To handle the
size() != 3
case, consider to use theMoreCollections.findOnlyElements()
method, which returnsOptional
.- Since:
- 6.6
-
combining
Returns a collector that collects the only four elements from the input and transforms them using themapper
function. If there are fewer or more elements in the input, IllegalArgumentExceptioin is thrown.To handle the
size() != 4
case, consider to use theMoreCollections.findOnlyElements()
method, which returnsOptional
.- Since:
- 6.6
-
combining
Returns a collector that collects the only five elements from the input and transforms them using themapper
function. If there are fewer or more elements in the input, IllegalArgumentExceptioin is thrown.To handle the
size() != 5
case, consider to use theMoreCollections.findOnlyElements()
method, which returnsOptional
.- Since:
- 6.6
-
combining
Returns a collector that collects the only six elements from the input and transforms them using themapper
function. If there are fewer or more elements in the input, IllegalArgumentExceptioin is thrown.To handle the
size() != 6
case, consider to use theMoreCollections.findOnlyElements()
method, which returnsOptional
.- Since:
- 6.6
-
combining
Returns a collector that collects the only seven elements from the input and transforms them using themapper
function. If there are fewer or more elements in the input, IllegalArgumentExceptioin is thrown.To handle the
size() != 7
case, consider to use theMoreCollections.findOnlyElements()
method, which returnsOptional
.- Since:
- 7.2
-
combining
Returns a collector that collects the only eight elements from the input and transforms them using themapper
function. If there are fewer or more elements in the input, IllegalArgumentExceptioin is thrown.To handle the
size() != 8
case, consider to use theMoreCollections.findOnlyElements()
method, which returnsOptional
.- Since:
- 7.2
-
onlyElement
Returns collector that collects the single element from the input. It will throw otherwise.- Since:
- 5.4
-
onlyElements
public static <T,R> FixedSizeCollector<T,?, onlyElementsR> (BiFunction<? super T, ? super T, ? extends R> mapper) Same ascombining(BiFunction)
.- Since:
- 5.3
-
onlyElements
Same ascombining(MapFrom3)
.- Since:
- 5.3
-
onlyElements
Same ascombining(MapFrom4)
.- Since:
- 5.3
-
onlyElements
Same ascombining(MapFrom5)
.- Since:
- 5.3
-
onlyElements
Same ascombining(MapFrom6)
.- Since:
- 5.3
-
onlyElements
Same ascombining(MapFrom7)
.- Since:
- 7.2
-
onlyElements
Same ascombining(MapFrom7)
.- Since:
- 7.2
-
switching
@SafeVarargs public static <T,R> Collector<T,?, switchingR> (FixedSizeCollector<T, ?, R> firstCase, FixedSizeCollector<T, ?, R>... moreCases) Returns aCollector
that will collect the input elements using the first of[firstCase, moreCases...]
that matches the input elements.For example, you may have a table name that could be in one of several formats:
database.schema.table
;schema.table
in the current database;table
in the current database and current schema;
Substring.first('.').repeatedly().split(tableName) .collect( switching( onlyElements((db, schema, table) -> ...), onlyElements((schema, table) -> ...), onlyElements(table -> ...)));
- Since:
- 5.4
-
partitioningBy
public static <E,R> Collector<E,?, partitioningByBoth<R, R>> (Predicate<? super E> predicate, Collector<E, ?, ? extends R> downstream) Returns a collector that partitions the incoming elements into two groups: elements that matchpredicate
, and those that don't.For example:
candidates .collect(partitioningBy(Candidate::isEligible, toImmutableList())) .andThen((eligible, ineligible) -> ...);
Compared to
Collectors.partitioningBy(java.util.function.Predicate<? super T>)
, which returns aMap<Boolean, V>
, the syntax is easier to be chained without needing an intermediaryMap
local variable.- Type Parameters:
E
- the input element typeR
- the result type ofdownstream
collector.- Since:
- 6.0
-
partitioningBy
public static <E,A1, Collector<E,A2, T, F> ?, partitioningByBoth<T, F>> (Predicate<? super E> predicate, Collector<E, A1, ? extends T> downstreamIfTrue, Collector<E, A2, ? extends F> downstreamIfFalse) Returns a collector that partitions the incoming elements into two groups: elements that matchpredicate
, and those that don't, and usedownstreamIfTrue
anddownstreamIfFalse
respectively to collect the elements.For example:
candidates .collect(partitioningBy(Candidate::isPrimary, toOptional(), toImmutableList())) .andThen((primary, secondaries) -> ...);
Compared to
Collectors.partitioningBy(java.util.function.Predicate<? super T>)
, which returns aMap<Boolean, V>
, the syntax is easier to be chained without needing an intermediaryMap
local variable; and you can collect the two partitions to different types.- Type Parameters:
E
- the input typeA1
- the accumulator type of thedownstreamIfTrue
collectorA2
- the accumulator type of thedownstreamIfFalse
collectorT
- the result type of thedownstreamIfTrue
collectorF
- the result type of thedownstreamIfFalse
collector- Since:
- 6.5
-
minMax
Returns a collector that collects the minimum and maximum elements from the input elements. the resultBiOptional
, if present, contains the pair of(min, max)
.Null elements are supported as long as
comparator
supports them.- Since:
- 6.0
-
allMin
public static <T,R> Collector<T,?, allMinR> (Comparator<? super T> comparator, Collector<? super T, ?, R> downstream) Returns aCollector
that collects all of the least (relative to the specifiedComparator
) input elements, in encounter order, using thedownstream
collector.For example:
Stream.of("foo", "bar", "banana", "papaya") .collect(allMin(comparingInt(String::length), toImmutableList())) // returns {"foo", "bar"}
- Since:
- 5.6
-
allMax
public static <T,R> Collector<T,?, allMaxR> (Comparator<? super T> comparator, Collector<? super T, ?, R> downstream) Returns aCollector
that collects all of the greatest (relative to the specifiedComparator
) input elements, in encounter order, using thedownstream
collector.For example:
Stream.of("foo", "quux", "banana", "papaya") .collect(greatest(comparingInt(String::length), toImmutableList())) // returns {"banana", "papaya"}
- Since:
- 5.6
-