Class MoreCollectors
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T,R> Collector <T, ?, R> allMax(Comparator<? super T> comparator, Collector<? super T, ?, R> downstream) Returns aCollectorthat collects all of the greatest (relative to the specifiedComparator) input elements, in encounter order, using thedownstreamcollector.static <T,R> Collector <T, ?, R> allMin(Comparator<? super T> comparator, Collector<? super T, ?, R> downstream) Returns aCollectorthat collects all of the least (relative to the specifiedComparator) input elements, in encounter order, using thedownstreamcollector.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 themapperfunction.static <T,R> FixedSizeCollector <T, ?, R> Returns a collector that collects the only four elements from the input and transforms them using themapperfunction.static <T,R> FixedSizeCollector <T, ?, R> Returns a collector that collects the only five elements from the input and transforms them using themapperfunction.static <T,R> FixedSizeCollector <T, ?, R> Returns a collector that collects the only six elements from the input and transforms them using themapperfunction.static <T,R> FixedSizeCollector <T, ?, R> Returns a collector that collects the only seven elements from the input and transforms them using themapperfunction.static <T,R> FixedSizeCollector <T, ?, R> Returns a collector that collects the only eight elements from the input and transforms them using themapperfunction.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 themapperfunction.static <T,K, V, R> Collector <T, ?, R> flatMapping(Function<? super T, ? extends BiStream<? extends K, ? extends V>> flattener, BiCollector<? super K, ? super 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 aCollectorthat first flattens the input stream of pairs (as opposed to single elements) and then collects the flattened pairs with thedownstreamBiCollector.flatteningMaps(BiCollector<K, V, R> downstream) Returns aCollectorthat flattens the inputMapentries and collects them using thedownstreamBiCollector.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 usesinputMapperfunction to transform each input, and finally usesoutputMapperto 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<? super A, ? super B, R> downstream) Analogous toCollectors.mapping(), applies a mapping function to each input element before accumulation, except that themapperfunction 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 usedownstreamIfTrueanddownstreamIfFalserespectively to collect the elements.static <T,R> Collector <T, ?, R> switching(FixedSizeCollector<T, ?, R> firstCase, FixedSizeCollector<T, ?, R>... moreCases) Returns aCollectorthat 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 thearrangerfunction 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 aCollectorthat extracts the keys and values through the givenkeyFunctionandvalueFunctionrespectively, and then collects them into a mutableMapcreated 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 usesinputMapperfunction to transform each input, and finally usesoutputMapperto 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<? super A, ? super B, R> downstream) Analogous toCollectors.mapping(), applies a mapping function to each input element before accumulation, except that themapperfunction 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<? super K, ? super 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 aCollectorthat first flattens the input stream of pairs (as opposed to single elements) and then collects the flattened pairs with thedownstreamBiCollector. -
flatteningMaps
Returns aCollectorthat flattens the inputMapentries and collects them using thedownstreamBiCollector.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, V, M extends Map<K,V>> Collector<T,?, toMapM> (Function<? super T, ? extends K> keyFunction, Function<? super T, ? extends V> valueFunction, Supplier<? extends M> mapSupplier) Returns aCollectorthat extracts the keys and values through the givenkeyFunctionandvalueFunctionrespectively, and then collects them into a mutableMapcreated bymapSupplier.Duplicate keys will cause
IllegalArgumentExceptionto be thrown, with the offending key reported in the error message.Null keys and values are discouraged but supported as long as the result
Mapsupports 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 thearrangerfunction 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 aCollectorthat maps the result ofupstreamcollector using thefinisherBiFunction. 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 themapperfunction. If there are fewer or more elements in the input, IllegalArgumentExceptioin is thrown.To handle the
size() != 2case, 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 themapperfunction. If there are fewer or more elements in the input, IllegalArgumentExceptioin is thrown.To handle the
size() != 3case, 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 themapperfunction. If there are fewer or more elements in the input, IllegalArgumentExceptioin is thrown.To handle the
size() != 4case, 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 themapperfunction. If there are fewer or more elements in the input, IllegalArgumentExceptioin is thrown.To handle the
size() != 5case, 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 themapperfunction. If there are fewer or more elements in the input, IllegalArgumentExceptioin is thrown.To handle the
size() != 6case, 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 themapperfunction. If there are fewer or more elements in the input, IllegalArgumentExceptioin is thrown.To handle the
size() != 7case, 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 themapperfunction. If there are fewer or more elements in the input, IllegalArgumentExceptioin is thrown.To handle the
size() != 8case, 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 aCollectorthat 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.tablein the current database;tablein 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, ?, Both<R,R>> 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.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 intermediaryMaplocal variable.- Type Parameters:
E- the input element typeR- the result type ofdownstreamcollector.- Since:
- 6.0
-
partitioningBy
public static <E,A1, Collector<E, ?, Both<T,A2, T, F> F>> 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 usedownstreamIfTrueanddownstreamIfFalserespectively 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 intermediaryMaplocal variable; and you can collect the two partitions to different types.- Type Parameters:
E- the input typeA1- the accumulator type of thedownstreamIfTruecollectorA2- the accumulator type of thedownstreamIfFalsecollectorT- the result type of thedownstreamIfTruecollectorF- the result type of thedownstreamIfFalsecollector- 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
comparatorsupports them.- Since:
- 6.0
-
allMin
public static <T,R> Collector<T,?, allMinR> (Comparator<? super T> comparator, Collector<? super T, ?, R> downstream) Returns aCollectorthat collects all of the least (relative to the specifiedComparator) input elements, in encounter order, using thedownstreamcollector.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 aCollectorthat collects all of the greatest (relative to the specifiedComparator) input elements, in encounter order, using thedownstreamcollector.For example:
Stream.of("foo", "quux", "banana", "papaya") .collect(greatest(comparingInt(String::length), toImmutableList())) // returns {"banana", "papaya"}- Since:
- 5.6
-