public final class Collectors
extends java.lang.Object
Collector
that implement various useful reduction
operations, such as accumulating elements into collections, summarizing
elements according to various criteria, etc.
The following are examples of using the predefined Collector
implementations in Collectors
with the Stream
API to perform
mutable reduction tasks:
// Accumulate elements into a List
List list = stream.collect(Collectors.toList());
// Accumulate elements into a TreeSet
List list = stream.collect(Collectors.toCollection(TreeSet::new));
// Convert elements to strings and concatenate them, separated by commas
String joined = stream.map(Object::toString)
.collect(Collectors.toStringJoiner(", "))
.toString();
// Find highest-paid employee
Employee highestPaid = employees.stream()
.collect(Collectors.maxBy(Comparators.comparing(Employee::getSalary)));
// Group employees by department
Map<Department, List<Employee>> byDept
= employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment));
// Find highest-paid employee by department
Map<Department, Employee> highestPaidByDept
= employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.maxBy(Comparators.comparing(Employee::getSalary))));
// Partition students into passing and failing
Map<Boolean, List<Student>> passingFailing =
students.stream()
.collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD);
TODO explanation of parallel collectionModifier and Type | Method and Description |
---|---|
static <T> Collector<T,java.lang.Long> |
counting()
Returns a
Collector<T, Long> that counts the number of input
elements. |
static <T> java.util.function.BinaryOperator<T> |
firstWinsMerger()
Returns a merge function, suitable for use in
Map.merge() or
toMap() ,
which implements a "first wins" policy. |
static <T,K> Collector<T,java.util.Map<K,java.util.List<T>>> |
groupingBy(java.util.function.Function<? super T,? extends K> classifier)
Returns a
Collector implementing a "group by" operation on
input elements of type T , grouping elements according to a
classification function. |
static <T,K,D> Collector<T,java.util.Map<K,D>> |
groupingBy(java.util.function.Function<? super T,? extends K> classifier,
Collector<T,D> downstream)
Returns a
Collector implementing a cascaded "group by" operation
on input elements of type T , grouping elements according to a
classification function, and then performing a reduction operation on
the values associated with a given key using the specified downstream
Collector . |
static <T,K,D,M extends java.util.Map<K,D>> |
groupingBy(java.util.function.Function<? super T,? extends K> classifier,
java.util.function.Supplier<M> mapFactory,
Collector<T,D> downstream)
Returns a
Collector implementing a cascaded "group by" operation
on input elements of type T , grouping elements according to a
classification function, and then performing a reduction operation on
the values associated with a given key using the specified downstream
Collector . |
static <T,K> Collector<T,java.util.concurrent.ConcurrentMap<K,java.util.List<T>>> |
groupingByConcurrent(java.util.function.Function<? super T,? extends K> classifier)
Returns a
Collector implementing a concurrent "group by"
operation on input elements of type T , grouping elements
according to a classification function. |
static <T,K,D> Collector<T,java.util.concurrent.ConcurrentMap<K,D>> |
groupingByConcurrent(java.util.function.Function<? super T,? extends K> classifier,
Collector<T,D> downstream)
Returns a
Collector implementing a concurrent cascaded "group by"
operation on input elements of type T , grouping elements
according to a classification function, and then performing a reduction
operation on the values associated with a given key using the specified
downstream Collector . |
static <T,K,D,M extends java.util.concurrent.ConcurrentMap<K,D>> |
groupingByConcurrent(java.util.function.Function<? super T,? extends K> classifier,
java.util.function.Supplier<M> mapFactory,
Collector<T,D> downstream)
Returns a concurrent
Collector implementing a cascaded "group by"
operation on input elements of type T , grouping elements
according to a classification function, and then performing a reduction
operation on the values associated with a given key using the specified
downstream Collector . |
static <T> java.util.function.BinaryOperator<T> |
lastWinsMerger()
Returns a merge function, suitable for use in
Map.merge() or
toMap() ,
which implements a "last wins" policy. |
static <T,U,R> Collector<T,R> |
mapping(java.util.function.Function<? super T,? extends U> mapper,
Collector<U,R> downstream)
Adapts a
Collector<U,R> to a Collector<T,R> by applying
a mapping function to each input element before accumulation. |
static <T> Collector<T,T> |
maxBy(java.util.Comparator<? super T> comparator)
Returns a
Collector<T, T> that produces the maximal element
according to a given Comparator . |
static <T> Collector<T,T> |
minBy(java.util.Comparator<? super T> comparator)
Returns a
Collector<T, T> that produces the minimal element
according to a given Comparator . |
static <T> Collector<T,java.util.Map<java.lang.Boolean,java.util.List<T>>> |
partitioningBy(java.util.function.Predicate<? super T> predicate)
Returns a
Collector which partitions the input elements according
to a Predicate , and organizes them into a
Map<Boolean, List<T>> . |
static <T,D> Collector<T,java.util.Map<java.lang.Boolean,D>> |
partitioningBy(java.util.function.Predicate<? super T> predicate,
Collector<T,D> downstream)
Returns a
Collector which partitions the input elements according
to a Predicate , reduces the values in each partition according to
another Collector , and organizes them into a
Map<Boolean, D> whose values are the result of the downstream
reduction. |
static <T> Collector<T,T> |
reducing(java.util.function.BinaryOperator<T> op)
Returns a
Collector<T,T> which performs a reduction of its
input elements under a specified BinaryOperator . |
static <T> Collector<T,T> |
reducing(T identity,
java.util.function.BinaryOperator<T> op)
Returns a
Collector<T,T> which performs a reduction of its
input elements under a specified BinaryOperator . |
static <T,U> Collector<T,U> |
reducing(U identity,
java.util.function.Function<? super T,? extends U> mapper,
java.util.function.BinaryOperator<U> op)
Returns a
Collector<T,U> which performs a reduction of its
input elements under a specified mapping function and
BinaryOperator . |
static <T> Collector<T,java.lang.Long> |
sumBy(java.util.function.Function<? super T,java.lang.Long> mapper)
Returns a
Collector<T, Long> that produces the sum of a
long-valued function applied to the input element. |
static <T> java.util.function.BinaryOperator<T> |
throwingMerger()
Returns a merge function, suitable for use in
Map.merge() or
toMap() , which always
throws IllegalStateException . |
static <T,C extends java.util.Collection<T>> |
toCollection(java.util.function.Supplier<C> collectionFactory)
Returns a
Collector that accumulates the input elements into a
new Collection , in encounter order. |
static <T,K,U> Collector<T,java.util.concurrent.ConcurrentMap<K,U>> |
toConcurrentMap(java.util.function.Function<? super T,? extends K> keyMapper,
java.util.function.Function<? super T,? extends U> valueMapper)
Accumulate elements into a
ConcurrentMap whose keys and values
are the result of applying mapping functions to the input elements. |
static <T,K,U> Collector<T,java.util.concurrent.ConcurrentMap<K,U>> |
toConcurrentMap(java.util.function.Function<? super T,? extends K> keyMapper,
java.util.function.Function<? super T,? extends U> valueMapper,
java.util.function.BinaryOperator<U> mergeFunction)
Accumulate elements into a
ConcurrentMap whose keys and values
are the result of applying mapping functions to the input elements. |
static <T,K,U,M extends java.util.concurrent.ConcurrentMap<K,U>> |
toConcurrentMap(java.util.function.Function<? super T,? extends K> keyMapper,
java.util.function.Function<? super T,? extends U> valueMapper,
java.util.function.BinaryOperator<U> mergeFunction,
java.util.function.Supplier<M> mapSupplier)
Accumulate elements into a
ConcurrentMap whose keys and values
are the result of applying mapping functions to the input elements. |
static <T> Collector<T,java.util.DoubleSummaryStatistics> |
toDoubleSummaryStatistics(java.util.function.ToDoubleFunction<? super T> mapper)
Returns a
Collector which applies an double -producing
mapping function to each input element, and returns summary statistics
for the resulting values. |
static <T> Collector<T,java.util.IntSummaryStatistics> |
toIntSummaryStatistics(java.util.function.ToIntFunction<? super T> mapper)
Returns a
Collector which applies an int -producing
mapping function to each input element, and returns summary statistics
for the resulting values. |
static <T> Collector<T,java.util.List<T>> |
toList()
Returns a
Collector that accumulates the input elements into a
new List . |
static <T> Collector<T,java.util.LongSummaryStatistics> |
toLongSummaryStatistics(java.util.function.ToLongFunction<? super T> mapper)
Returns a
Collector which applies an long -producing
mapping function to each input element, and returns summary statistics
for the resulting values. |
static <T,K,U> Collector<T,java.util.Map<K,U>> |
toMap(java.util.function.Function<? super T,? extends K> keyMapper,
java.util.function.Function<? super T,? extends U> valueMapper)
Accumulate elements into a
Map whose keys and values are the
result of applying mapping functions to the input elements. |
static <T,K,U> Collector<T,java.util.Map<K,U>> |
toMap(java.util.function.Function<? super T,? extends K> keyMapper,
java.util.function.Function<? super T,? extends U> valueMapper,
java.util.function.BinaryOperator<U> mergeFunction)
Accumulate elements into a
Map whose keys and values are the
result of applying mapping functions to the input elements. |
static <T,K,U,M extends java.util.Map<K,U>> |
toMap(java.util.function.Function<? super T,? extends K> keyMapper,
java.util.function.Function<? super T,? extends U> valueMapper,
java.util.function.BinaryOperator<U> mergeFunction,
java.util.function.Supplier<M> mapSupplier)
Accumulate elements into a
Map whose keys and values are the
result of applying mapping functions to the input elements. |
static <T> Collector<T,java.util.Set<T>> |
toSet()
Returns a
Collector that accumulates the input elements into a
new Set . |
static Collector<java.lang.String,java.lang.StringBuilder> |
toStringBuilder()
Returns a
Collector that concatenates the input elements into a
new StringBuilder . |
static Collector<java.lang.CharSequence,java.util.StringJoiner> |
toStringJoiner(java.lang.String separator)
Returns a
Collector that concatenates the input elements into a
new StringJoiner , using the specified separator. |
public static <T> java.util.function.BinaryOperator<T> throwingMerger()
Map.merge()
or
toMap()
, which always
throws IllegalStateException
. This can be used to enforce the
assumption that the elements being collected are distinct.T
- The type of input arguments to the merge functionIllegalStateException
firstWinsMerger()
,
lastWinsMerger()
public static <T> java.util.function.BinaryOperator<T> firstWinsMerger()
Map.merge()
or
toMap()
,
which implements a "first wins" policy.T
- The type of input arguments to the merge functionlastWinsMerger()
,
throwingMerger()
public static <T> java.util.function.BinaryOperator<T> lastWinsMerger()
Map.merge()
or
toMap()
,
which implements a "last wins" policy.T
- The type of input arguments to the merge functionfirstWinsMerger()
,
throwingMerger()
public static <T,C extends java.util.Collection<T>> Collector<T,C> toCollection(java.util.function.Supplier<C> collectionFactory)
Collector
that accumulates the input elements into a
new Collection
, in encounter order. The Collection
is
created by the provided factory.T
- The type of the input elementsC
- The type of the resulting Collection
collectionFactory
- A Supplier
which returns a new, empty
Collection
of the appropriate typeCollector
which collects all the input elements into a
Collection
, in encounter orderpublic static <T> Collector<T,java.util.List<T>> toList()
Collector
that accumulates the input elements into a
new List
. There are no guarantees on the type, mutability,
serializability, or thread-safety of the List
returned.T
- The type of the input elementsCollector
which collects all the input elements into a
List
, in encounter orderpublic static <T> Collector<T,java.util.Set<T>> toSet()
Collector
that accumulates the input elements into a
new Set
. There are no guarantees on the type, mutability,
serializability, or thread-safety of the Set
returned.T
- The type of the input elementsCollector
which collects all the input elements into a
Set
public static Collector<java.lang.String,java.lang.StringBuilder> toStringBuilder()
Collector
that concatenates the input elements into a
new StringBuilder
.Collector
which collects String elements into a
StringBuilder
, in encounter orderpublic static Collector<java.lang.CharSequence,java.util.StringJoiner> toStringJoiner(java.lang.String separator)
Collector
that concatenates the input elements into a
new StringJoiner
, using the specified separator.separator
- the separator to be used between each element
added to the StringJoinerCollector
which collects String elements into a
StringJoiner
, in encounter orderpublic static <T,U,R> Collector<T,R> mapping(java.util.function.Function<? super T,? extends U> mapper, Collector<U,R> downstream)
Collector<U,R>
to a Collector<T,R>
by applying
a mapping function to each input element before accumulation.T
- The type of the input elementsU
- Type of elements accepted by downstream collectorR
- Result type of collectormapper
- A function to be applied to the input elementsdownstream
- A collector which will accept mapped valuesmapping()
collectors are most useful when used in a
multi-level reduction, downstream of groupingBy
or
partitioningBy
. For example, given a stream of
Person
, to accumulate the set of last names in each city:
Map<City, Set<String>> lastNamesByCity
= people.stream().collect(groupingBy(Person::getCity,
mapping(Person::getLastName, toSet())));
public static <T> Collector<T,java.lang.Long> counting()
Collector<T, Long>
that counts the number of input
elements.T
- The type of the input elementsCollector
that counts the input elementsreducing(0L, e -> 1L, Long::sum)
public static <T> Collector<T,T> minBy(java.util.Comparator<? super T> comparator)
Collector<T, T>
that produces the minimal element
according to a given Comparator
.T
- The type of the input elementscomparator
- A Comparator
for comparing elementsCollector
that produces the minimal valuereducing(Comparators.lesserOf(comparator))
public static <T> Collector<T,T> maxBy(java.util.Comparator<? super T> comparator)
Collector<T, T>
that produces the maximal element
according to a given Comparator
.T
- The type of the input elementscomparator
- A Comparator
for comparing elementsCollector
that produces the maximal valuereducing(Comparators.greaterOf(comparator))
public static <T> Collector<T,java.lang.Long> sumBy(java.util.function.Function<? super T,java.lang.Long> mapper)
Collector<T, Long>
that produces the sum of a
long-valued function applied to the input element.T
- The type of the input elementsmapper
- A function extracting the property to be summedCollector
that produces the sum of a derived propertyreducing(0L, mapper, Long::sum)
public static <T> Collector<T,T> reducing(T identity, java.util.function.BinaryOperator<T> op)
Collector<T,T>
which performs a reduction of its
input elements under a specified BinaryOperator
.T
- Element type for the input and output of the reductionidentity
- The identity value for the reduction (also, the value
that is returned when there are no input elements)op
- A BinaryOperator<T>
used to reduce the input elementsCollector
which implements the reduction operationreducing(BinaryOperator)
,
reducing(Object, Function, BinaryOperator)
reducing()
collectors are most useful when used in a
multi-level reduction, downstream of groupingBy
or
partitioningBy
. To perform a simple reduction on a stream,
use Stream.reduce(BinaryOperator)
instead.public static <T> Collector<T,T> reducing(java.util.function.BinaryOperator<T> op)
Collector<T,T>
which performs a reduction of its
input elements under a specified BinaryOperator
.T
- Element type for the input and output of the reductionop
- A BinaryOperator<T>
used to reduce the input elementsCollector
which implements the reduction operationreducing(Object, BinaryOperator)
,
reducing(Object, Function, BinaryOperator)
reducing()
collectors are most useful when used in a
multi-level reduction, downstream of groupingBy
or
partitioningBy
. To perform a simple reduction on a stream,
use Stream.reduce(BinaryOperator)
instead.
For example, given a stream of Person
, to calculate tallest
person in each city:
Comparator<Person> byHeight = Comparators.comparing(Person::getHeight);
BinaryOperator<Person> tallerOf = Comparators.greaterOf(byHeight);
Map<City, Person> tallestByCity
= people.stream().collect(groupingBy(Person::getCity, reducing(tallerOf)));
public static <T,U> Collector<T,U> reducing(U identity, java.util.function.Function<? super T,? extends U> mapper, java.util.function.BinaryOperator<U> op)
Collector<T,U>
which performs a reduction of its
input elements under a specified mapping function and
BinaryOperator
. This is a generalization of
reducing(Object, BinaryOperator)
which allows a transformation
of the elements before reduction.T
- The type of the input elementsU
- The type of the mapped valuesidentity
- The identity value for the reduction (also, the value
that is returned when there are no input elements)mapper
- A mapping function to apply to each input valueop
- A BinaryOperator<U>
used to reduce the mapped valuesCollector
implementing the map-reduce operationreducing(Object, BinaryOperator)
,
reducing(BinaryOperator)
reducing()
collectors are most useful when used in a
multi-level reduction, downstream of groupingBy
or
partitioningBy
. To perform a simple reduction on a stream,
use Stream.reduce(BinaryOperator)
instead.
For example, given a stream of Person
, to calculate the longest
last name of residents in each city:
Comparator<String> byLength = Comparators.comparing(String::length);
BinaryOperator<String> longerOf = Comparators.greaterOf(byLength);
Map<City, String> longestLastNameByCity
= people.stream().collect(groupingBy(Person::getCity,
reducing(Person::getLastName, longerOf)));
public static <T,K> Collector<T,java.util.Map<K,java.util.List<T>>> groupingBy(java.util.function.Function<? super T,? extends K> classifier)
Collector
implementing a "group by" operation on
input elements of type T
, grouping elements according to a
classification function.
The classification function maps elements to some key type K
.
The collector produces a Map<K, List<T>>
whose keys are the
values resulting from applying the classification function to the input
elements, and whose corresponding values are List
s containing the
input elements which map to the associated key under the classification
function.
There are no guarantees on the type, mutability, serializability, or
thread-safety of the Map
or List
objects returned.
T
- The type of the input elementsK
- The type of the keysclassifier
- The classifier function mapping input elements to keysCollector
implementing the group-by operationgroupingBy(Function, Collector)
,
groupingBy(Function, Supplier, Collector)
,
groupingByConcurrent(Function)
groupingBy(classifier, toList());
public static <T,K,D> Collector<T,java.util.Map<K,D>> groupingBy(java.util.function.Function<? super T,? extends K> classifier, Collector<T,D> downstream)
Collector
implementing a cascaded "group by" operation
on input elements of type T
, grouping elements according to a
classification function, and then performing a reduction operation on
the values associated with a given key using the specified downstream
Collector
.
The classification function maps elements to some key type K
.
The downstream collector operates on elements of type T
and
produces a result of type D
. The resulting collector produces a
Map<K, D>
.
For example, to compute the set of last names of people in each city:
Map<City, Set<String>> namesByCity
= people.stream().collect(groupingBy(Person::getCity,
mapping(Person::getLastName, toSet())));
There are no guarantees on the type, mutability,
serializability, or thread-safety of the Map
returned.T
- The type of the input elementsK
- The type of the keysD
- The result type of the downstream reductionclassifier
- The classifier function mapping input elements to keysdownstream
- A Collector
implementing the downstream reductionCollector
implementing the cascaded group-by operationgroupingBy(Function)
,
groupingBy(Function, Supplier, Collector)
,
groupingByConcurrent(Function, Collector)
public static <T,K,D,M extends java.util.Map<K,D>> Collector<T,M> groupingBy(java.util.function.Function<? super T,? extends K> classifier, java.util.function.Supplier<M> mapFactory, Collector<T,D> downstream)
Collector
implementing a cascaded "group by" operation
on input elements of type T
, grouping elements according to a
classification function, and then performing a reduction operation on
the values associated with a given key using the specified downstream
Collector
. The Map
produced by the Collector is created
with the supplied factory function.
The classification function maps elements to some key type K
.
The downstream collector operates on elements of type T
and
produces a result of type D
. The resulting collector produces a
Map<K, D>
.
For example, to compute the set of last names of people in each city, where the city names are sorted:
Map<City, Set<String>> namesByCity
= people.stream().collect(groupingBy(Person::getCity, TreeMap::new,
mapping(Person::getLastName, toSet())));
T
- The type of the input elementsK
- The type of the keysD
- The result type of the downstream reductionM
- The type of the resulting Map
classifier
- The classifier function mapping input elements to keysdownstream
- A Collector
implementing the downstream reductionmapFactory
- A function which, when called, produces a new empty
Map
of the desired typeCollector
implementing the cascaded group-by operationgroupingBy(Function, Collector)
,
groupingBy(Function)
,
groupingByConcurrent(Function, Supplier, Collector)
public static <T,K> Collector<T,java.util.concurrent.ConcurrentMap<K,java.util.List<T>>> groupingByConcurrent(java.util.function.Function<? super T,? extends K> classifier)
Collector
implementing a concurrent "group by"
operation on input elements of type T
, grouping elements
according to a classification function.
This is a concurrent
and
unordered
Collector.
The classification function maps elements to some key type K
.
The collector produces a ConcurrentMap<K, List<T>>
whose keys are the
values resulting from applying the classification function to the input
elements, and whose corresponding values are List
s containing the
input elements which map to the associated key under the classification
function.
There are no guarantees on the type, mutability, serializability, or
thread-safety of the Map
or List
objects returned.
T
- The type of the input elementsK
- The type of the keysclassifier
- The classifier function mapping input elements to keysCollector
implementing the group-by operationgroupingBy(Function)
,
groupingByConcurrent(Function, Collector)
,
groupingByConcurrent(Function, Supplier, Collector)
groupingByConcurrent(classifier, toList());
public static <T,K,D> Collector<T,java.util.concurrent.ConcurrentMap<K,D>> groupingByConcurrent(java.util.function.Function<? super T,? extends K> classifier, Collector<T,D> downstream)
Collector
implementing a concurrent cascaded "group by"
operation on input elements of type T
, grouping elements
according to a classification function, and then performing a reduction
operation on the values associated with a given key using the specified
downstream Collector
.
This is a concurrent
and
unordered
Collector.
The classification function maps elements to some key type K
.
The downstream collector operates on elements of type T
and
produces a result of type D
. The resulting collector produces a
Map<K, D>
.
For example, to compute the set of last names of people in each city, where the city names are sorted:
ConcurrentMap<City, Set<String>> namesByCity
= people.stream().collect(groupingByConcurrent(Person::getCity, TreeMap::new,
mapping(Person::getLastName, toSet())));
T
- The type of the input elementsK
- The type of the keysD
- The result type of the downstream reductionclassifier
- The classifier function mapping input elements to keysdownstream
- A Collector
implementing the downstream reductionCollector
implementing the cascaded group-by operationgroupingBy(Function, Collector)
,
groupingByConcurrent(Function)
,
groupingByConcurrent(Function, Supplier, Collector)
public static <T,K,D,M extends java.util.concurrent.ConcurrentMap<K,D>> Collector<T,M> groupingByConcurrent(java.util.function.Function<? super T,? extends K> classifier, java.util.function.Supplier<M> mapFactory, Collector<T,D> downstream)
Collector
implementing a cascaded "group by"
operation on input elements of type T
, grouping elements
according to a classification function, and then performing a reduction
operation on the values associated with a given key using the specified
downstream Collector
. The ConcurrentMap
produced by the
Collector is created with the supplied factory function.
This is a concurrent
and
unordered
Collector.
The classification function maps elements to some key type K
.
The downstream collector operates on elements of type T
and
produces a result of type D
. The resulting collector produces a
Map<K, D>
.
For example, to compute the set of last names of people in each city, where the city names are sorted:
ConcurrentMap<City, Set<String>> namesByCity
= people.stream().collect(groupingBy(Person::getCity, ConcurrentSkipListMap::new,
mapping(Person::getLastName, toSet())));
T
- The type of the input elementsK
- The type of the keysD
- The result type of the downstream reductionM
- The type of the resulting ConcurrentMap
classifier
- The classifier function mapping input elements to keysdownstream
- A Collector
implementing the downstream reductionmapFactory
- A function which, when called, produces a new empty
ConcurrentMap
of the desired typeCollector
implementing the cascaded group-by operationgroupingByConcurrent(Function)
,
groupingByConcurrent(Function, Collector)
,
groupingBy(Function, Supplier, Collector)
public static <T> Collector<T,java.util.Map<java.lang.Boolean,java.util.List<T>>> partitioningBy(java.util.function.Predicate<? super T> predicate)
Collector
which partitions the input elements according
to a Predicate
, and organizes them into a
Map<Boolean, List<T>>
.
There are no guarantees on the type, mutability,
serializability, or thread-safety of the Map
returned.T
- The type of the input elementspredicate
- The predicate used for classifying input elementsCollector
implementing the partitioning operation.partitioningBy(Predicate, Collector)
public static <T,D> Collector<T,java.util.Map<java.lang.Boolean,D>> partitioningBy(java.util.function.Predicate<? super T> predicate, Collector<T,D> downstream)
Collector
which partitions the input elements according
to a Predicate
, reduces the values in each partition according to
another Collector
, and organizes them into a
Map<Boolean, D>
whose values are the result of the downstream
reduction.
There are no guarantees on the type, mutability,
serializability, or thread-safety of the Map
returned.T
- The type of the input elementsD
- The result type of the downstream reductionpredicate
- The predicate used for classifying input elementsdownstream
- A Collector
implementing the downstream reductionCollector
implementing the cascaded partitioning operation.partitioningBy(Predicate)
public static <T,K,U> Collector<T,java.util.Map<K,U>> toMap(java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends U> valueMapper)
Map
whose keys and values are the
result of applying mapping functions to the input elements.
If the mapped keys contains duplicates (according to
Object.equals(Object)
), an IllegalStateException
is
thrown when the collection operation is performed. If the mapped keys
may have duplicates, use toMap(Function, Function, BinaryOperator)
instead.T
- The type of the input elementsK
- The output type of the key mapping functionU
- The output type of the value mapping functionkeyMapper
- The mapping function to produce keysvalueMapper
- The mapping function to produce valuesCollector
which collects elements into a Map
whose keys and values are the result of applying mapping functions to
the input elementstoMap(Function, Function, BinaryOperator)
,
toMap(Function, Function, BinaryOperator, Supplier)
,
toConcurrentMap(Function, Function)
Functions.identity()
may be helpful.
For example, the following produces a Map
mapping
students to their grade point average:
Map<Student, Double> studentToGPA
students.stream().collect(toMap(Functions.identity(),
student -> computeGPA(student)));
And the following produces a Map
mapping a unique identifier to
students:
Map<String, Student> studentIdToStudent
students.stream().collect(toMap(Student::getId,
Functions.identity());
public static <T,K,U> Collector<T,java.util.Map<K,U>> toMap(java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends U> valueMapper, java.util.function.BinaryOperator<U> mergeFunction)
Map
whose keys and values are the
result of applying mapping functions to the input elements. If the mapped
keys contains duplicates (according to Object.equals(Object)
),
the value mapping function is applied to each equal element, and the
results are merged using the provided merging function.T
- The type of the input elementsK
- The output type of the key mapping functionU
- The output type of the value mapping functionkeyMapper
- The mapping function to produce keysvalueMapper
- The mapping function to produce valuesmergeFunction
- A merge function, used to resolve collisions between
values associated with the same key, as supplied
to Map.merge(Object, Object, BiFunction)
Collector
which collects elements into a Map
whose keys are the result of applying a key mapping function to the input
elements, and whose values are the result of applying a value mapping
function to all input elements equal to the key and combining them
using the merge functiontoMap(Function, Function)
,
toMap(Function, Function, BinaryOperator, Supplier)
,
toConcurrentMap(Function, Function, BinaryOperator)
throwingMerger()
, firstWinsMerger()
, and
lastWinsMerger()
, that implement common policies, or you can
implement custom policies easily. For example, if you have a stream
of Person
, and you want to produce a "phone book" mapping name to
address, but it is possible that two persons have the same name, you can
do as follows to gracefully deals with these collisions, and produce a
Map
mapping names to a concatened list of addresses:
Map<String, String> phoneBook
people.stream().collect(toMap(Person::getName,
Person::getAddress,
(s, a) -> s + ", " + a));
public static <T,K,U,M extends java.util.Map<K,U>> Collector<T,M> toMap(java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends U> valueMapper, java.util.function.BinaryOperator<U> mergeFunction, java.util.function.Supplier<M> mapSupplier)
Map
whose keys and values are the
result of applying mapping functions to the input elements. If the mapped
keys contains duplicates (according to Object.equals(Object)
),
the value mapping function is applied to each equal element, and the
results are merged using the provided merging function. The Map
is created by a provided supplier function.T
- The type of the input elementsK
- The output type of the key mapping functionU
- The output type of the value mapping functionM
- The type of the resulting Map
keyMapper
- The mapping function to produce keysvalueMapper
- The mapping function to produce valuesmergeFunction
- A merge function, used to resolve collisions between
values associated with the same key, as supplied
to Map.merge(Object, Object, BiFunction)
mapSupplier
- A function which returns a new, empty Map
into
which the results will be insertedCollector
which collects elements into a Map
whose keys are the result of applying a key mapping function to the input
elements, and whose values are the result of applying a value mapping
function to all input elements equal to the key and combining them
using the merge functiontoMap(Function, Function)
,
toMap(Function, Function, BinaryOperator)
,
toConcurrentMap(Function, Function, BinaryOperator, Supplier)
public static <T,K,U> Collector<T,java.util.concurrent.ConcurrentMap<K,U>> toConcurrentMap(java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends U> valueMapper)
ConcurrentMap
whose keys and values
are the result of applying mapping functions to the input elements.
If the mapped keys contains duplicates (according to
Object.equals(Object)
), an IllegalStateException
is
thrown when the collection operation is performed. If the mapped keys
may have duplicates, use
toConcurrentMap(Function, Function, BinaryOperator)
instead.T
- The type of the input elementsK
- The output type of the key mapping functionU
- The output type of the value mapping functionkeyMapper
- The mapping function to produce keysvalueMapper
- The mapping function to produce valuesCollector
which collects elements into a
ConcurrentMap
whose keys are the result of applying a key mapping
function to the input elements, and whose values are the result of
applying a value mapping function to the input elementstoMap(Function, Function)
,
toConcurrentMap(Function, Function, BinaryOperator)
,
toConcurrentMap(Function, Function, BinaryOperator, Supplier)
Functions.identity()
may be helpful.
For example, the following produces a Map
mapping
students to their grade point average:
Map<Student, Double> studentToGPA
students.stream().collect(toMap(Functions.identity(),
student -> computeGPA(student)));
And the following produces a Map
mapping a unique identifier to
students:
Map<String, Student> studentIdToStudent
students.stream().collect(toConcurrentMap(Student::getId,
Functions.identity());
This is a concurrent
and
unordered
Collector.
public static <T,K,U> Collector<T,java.util.concurrent.ConcurrentMap<K,U>> toConcurrentMap(java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends U> valueMapper, java.util.function.BinaryOperator<U> mergeFunction)
ConcurrentMap
whose keys and values
are the result of applying mapping functions to the input elements. If
the mapped keys contains duplicates (according to Object.equals(Object)
),
the value mapping function is applied to each equal element, and the
results are merged using the provided merging function.T
- The type of the input elementsK
- The output type of the key mapping functionU
- The output type of the value mapping functionkeyMapper
- The mapping function to produce keysvalueMapper
- The mapping function to produce valuesmergeFunction
- A merge function, used to resolve collisions between
values associated with the same key, as supplied
to Map.merge(Object, Object, BiFunction)
Collector
which collects elements into a
ConcurrentMap
whose keys are the result of applying a key mapping
function to the input elements, and whose values are the result of
applying a value mapping function to all input elements equal to the key
and combining them using the merge functiontoConcurrentMap(Function, Function)
,
toConcurrentMap(Function, Function, BinaryOperator, Supplier)
,
toMap(Function, Function, BinaryOperator)
throwingMerger()
, firstWinsMerger()
, and
lastWinsMerger()
, that implement common policies, or you can
implement custom policies easily. For example, if you have a stream
of Person
, and you want to produce a "phone book" mapping name to
address, but it is possible that two persons have the same name, you can
do as follows to gracefully deals with these collisions, and produce a
Map
mapping names to a concatened list of addresses:
Map<String, String> phoneBook
people.stream().collect(toConcurrentMap(Person::getName,
Person::getAddress,
(s, a) -> s + ", " + a));
This is a concurrent
and
unordered
Collector.
public static <T,K,U,M extends java.util.concurrent.ConcurrentMap<K,U>> Collector<T,M> toConcurrentMap(java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends U> valueMapper, java.util.function.BinaryOperator<U> mergeFunction, java.util.function.Supplier<M> mapSupplier)
ConcurrentMap
whose keys and values
are the result of applying mapping functions to the input elements. If
the mapped keys contains duplicates (according to Object.equals(Object)
),
the value mapping function is applied to each equal element, and the
results are merged using the provided merging function. The
ConcurrentMap
is created by a provided supplier function.
This is a concurrent
and
unordered
Collector.
T
- The type of the input elementsK
- The output type of the key mapping functionU
- The output type of the value mapping functionM
- The type of the resulting ConcurrentMap
keyMapper
- The mapping function to produce keysvalueMapper
- The mapping function to produce valuesmergeFunction
- A merge function, used to resolve collisions between
values associated with the same key, as supplied
to Map.merge(Object, Object, BiFunction)
mapSupplier
- A function which returns a new, empty Map
into
which the results will be insertedCollector
which collects elements into a
ConcurrentMap
whose keys are the result of applying a key mapping
function to the input elements, and whose values are the result of
applying a value mapping function to all input elements equal to the key
and combining them using the merge functiontoConcurrentMap(Function, Function)
,
toConcurrentMap(Function, Function, BinaryOperator)
,
toMap(Function, Function, BinaryOperator, Supplier)
public static <T> Collector<T,java.util.IntSummaryStatistics> toIntSummaryStatistics(java.util.function.ToIntFunction<? super T> mapper)
Collector
which applies an int
-producing
mapping function to each input element, and returns summary statistics
for the resulting values.T
- The type of the input elementsmapper
- The mapping function to apply to each elementCollector
implementing the summary-statistics reductiontoDoubleSummaryStatistics(ToDoubleFunction)
,
toLongSummaryStatistics(ToLongFunction)
public static <T> Collector<T,java.util.LongSummaryStatistics> toLongSummaryStatistics(java.util.function.ToLongFunction<? super T> mapper)
Collector
which applies an long
-producing
mapping function to each input element, and returns summary statistics
for the resulting values.T
- The type of the input elementsmapper
- The mapping function to apply to each elementCollector
implementing the summary-statistics reductiontoDoubleSummaryStatistics(ToDoubleFunction)
,
toIntSummaryStatistics(ToIntFunction)
public static <T> Collector<T,java.util.DoubleSummaryStatistics> toDoubleSummaryStatistics(java.util.function.ToDoubleFunction<? super T> mapper)
Collector
which applies an double
-producing
mapping function to each input element, and returns summary statistics
for the resulting values.T
- The type of the input elementsmapper
- The mapping function to apply to each elementCollector
implementing the summary-statistics reductiontoLongSummaryStatistics(ToLongFunction)
,
toIntSummaryStatistics(ToIntFunction)