Collectors
public
final
class
Collectors
extends Object
| java.lang.Object | |
| ↳ | java.util.stream.Collectors |
Implementations of 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 collectors to perform common mutable reduction tasks:
// Accumulate names into a List
List<String> list = people.stream().map(Person::getName).collect(Collectors.toList());
// Accumulate names into a TreeSet
Set<String> set = people.stream().map(Person::getName).collect(Collectors.toCollection(TreeSet::new));
// Convert elements to strings and concatenate them, separated by commas
String joined = things.stream()
.map(Object::toString)
.collect(Collectors.joining(", "));
// Compute sum of salaries of employee
int total = employees.stream()
.collect(Collectors.summingInt(Employee::getSalary)));
// Group employees by department
Map<Department, List<Employee>> byDept
= employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment));
// Compute sum of salaries by department
Map<Department, Integer> totalByDept
= employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.summingInt(Employee::getSalary)));
// Partition students into passing and failing
Map<Boolean, List<Student>> passingFailing =
students.stream()
.collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD));
Summary
Public methods | |
|---|---|
static
<T>
Collector<T, ?, Double>
|
averagingDouble(ToDoubleFunction<? super T> mapper)
Returns a |
static
<T>
Collector<T, ?, Double>
|
averagingInt(ToIntFunction<? super T> mapper)
Returns a |
static
<T>
Collector<T, ?, Double>
|
averagingLong(ToLongFunction<? super T> mapper)
Returns a |
static
<T, A, R, RR>
Collector<T, A, RR>
|
collectingAndThen(Collector<T, A, R> downstream, Function<R, RR> finisher)
Adapts a |
static
<T>
Collector<T, ?, Long>
|
counting()
Returns a |
static
<T, K>
Collector<T, ?, Map<K, List<T>>>
|
groupingBy(Function<? super T, ? extends K> classifier)
Returns a |
static
<T, K, D, A, M extends Map<K, D>>
Collector<T, ?, M>
|
groupingBy(Function<? super T, ? extends K> classifier, Supplier<M> mapFactory, Collector<? super T, A, D> downstream)
Returns a |
static
<T, K, A, D>
Collector<T, ?, Map<K, D>>
|
groupingBy(Function<? super T, ? extends K> classifier, Collector<? super T, A, D> downstream)
Returns a |
static
<T, K, A, D>
Collector<T, ?, ConcurrentMap<K, D>>
|
groupingByConcurrent(Function<? super T, ? extends K> classifier, Collector<? super T, A, D> downstream)
Returns a concurrent |
static
<T, K>
Collector<T, ?, ConcurrentMap<K, List<T>>>
|
groupingByConcurrent(Function<? super T, ? extends K> classifier)
Returns a concurrent |
static
<T, K, A, D, M extends ConcurrentMap<K, D>>
Collector<T, ?, M>
|
groupingByConcurrent(Function<? super T, ? extends K> classifier, Supplier<M> mapFactory, Collector<? super T, A, D> downstream)
Returns a concurrent |
static
Collector<CharSequence, ?, String>
|
joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
Returns a |
static
Collector<CharSequence, ?, String>
|
joining(CharSequence delimiter)
Returns a |
static
Collector<CharSequence, ?, String>
|
joining()
Returns a |
static
<T, U, A, R>
Collector<T, ?, R>
|
mapping(Function<? super T, ? extends U> mapper, Collector<? super U, A, R> downstream)
Adapts a |
static
<T>
Collector<T, ?, Optional<T>>
|
maxBy(Comparator<? super T> comparator)
Returns a |
static
<T>
Collector<T, ?, Optional<T>>
|
minBy(Comparator<? super T> comparator)
Returns a |
static
<T>
Collector<T, ?, Map<Boolean, List<T>>>
|
partitioningBy(Predicate<? super T> predicate)
Returns a |
static
<T, D, A>
Collector<T, ?, Map<Boolean, D>>
|
partitioningBy(Predicate<? super T> predicate, Collector<? super T, A, D> downstream)
Returns a |
static
<T>
Collector<T, ?, Optional<T>>
|
reducing(BinaryOperator<T> op)
Returns a |
static
<T, U>
Collector<T, ?, U>
|
reducing(U identity, Function<? super T, ? extends U> mapper, BinaryOperator<U> op)
Returns a |
static
<T>
Collector<T, ?, T>
|
reducing(T identity, BinaryOperator<T> op)
Returns a |
static
<T>
Collector<T, ?, DoubleSummaryStatistics>
|
summarizingDouble(ToDoubleFunction<? super T> mapper)
Returns a |
static
<T>
Collector<T, ?, IntSummaryStatistics>
|
summarizingInt(ToIntFunction<? super T> mapper)
Returns a |
static
<T>
Collector<T, ?, LongSummaryStatistics>
|
summarizingLong(ToLongFunction<? super T> mapper)
Returns a |
static
<T>
Collector<T, ?, Double>
|
summingDouble(ToDoubleFunction<? super T> mapper)
Returns a |
static
<T>
Collector<T, ?, Integer>
|
summingInt(ToIntFunction<? super T> mapper)
Returns a |
static
<T>
Collector<T, ?, Long>
|
summingLong(ToLongFunction<? super T> mapper)
Returns a |
static
<T, C extends Collection<T>>
Collector<T, ?, C>
|
toCollection(Supplier<C> collectionFactory)
Returns a |
static
<T, K, U, M extends ConcurrentMap<K, U>>
Collector<T, ?, M>
|
toConcurrentMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier)
Returns a concurrent |
static
<T, K, U>
Collector<T, ?, ConcurrentMap<K, U>>
|
toConcurrentMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction)
Returns a concurrent |
static
<T, K, U>
Collector<T, ?, ConcurrentMap<K, U>>
|
toConcurrentMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)
Returns a concurrent |
static
<T>
Collector<T, ?, List<T>>
|
toList()
Returns a |
static
<T, K, U>
Collector<T, ?, Map<K, U>>
|
toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction)
Returns a |
static
<T, K, U, M extends Map<K, U>>
Collector<T, ?, M>
|
toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier)
Returns a |
static
<T, K, U>
Collector<T, ?, Map<K, U>>
|
toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)
Returns a |
static
<T>
Collector<T, ?, Set<T>>
|
toSet()
Returns a |
Inherited methods | |
|---|---|
Public methods
averagingDouble
public static Collector<T, ?, Double> averagingDouble (ToDoubleFunction<? super T> mapper)
Returns a Collector that produces the arithmetic mean of a double-valued
function applied to the input elements. If no elements are present,
the result is 0.
The average returned can vary depending upon the order in which
values are recorded, due to accumulated rounding error in
addition of values of differing magnitudes. Values sorted by increasing
absolute magnitude tend to yield more accurate results. If any recorded
value is a NaN or the sum is at any point a NaN then the
average will be NaN.
Implementation Note:
- The
doubleformat can represent all consecutive integers in the range -253 to 253. If the pipeline has more than 253 values, the divisor in the average computation will saturate at 253, leading to additional numerical errors.
| Parameters | |
|---|---|
mapper |
ToDoubleFunction: a function extracting the property to be summed |
| Returns | |
|---|---|
Collector<T, ?, Double> |
a Collector that produces the sum of a derived property |
averagingInt
public static Collector<T, ?, Double> averagingInt (ToIntFunction<? super T> mapper)
Returns a Collector that produces the arithmetic mean of an integer-valued
function applied to the input elements. If no elements are present,
the result is 0.
| Parameters | |
|---|---|
mapper |
ToIntFunction: a function extracting the property to be summed |
| Returns | |
|---|---|
Collector<T, ?, Double> |
a Collector that produces the sum of a derived property |
averagingLong
public static Collector<T, ?, Double> averagingLong (ToLongFunction<? super T> mapper)
Returns a Collector that produces the arithmetic mean of a long-valued
function applied to the input elements. If no elements are present,
the result is 0.
| Parameters | |
|---|---|
mapper |
ToLongFunction: a function extracting the property to be summed |
| Returns | |
|---|---|
Collector<T, ?, Double> |
a Collector that produces the sum of a derived property |
collectingAndThen
public static Collector<T, A, RR> collectingAndThen (Collector<T, A, R> downstream, Function<R, RR> finisher)
Adapts a Collector to perform an additional finishing
transformation. For example, one could adapt the toList()
collector to always produce an immutable list with:
List<String> people
= people.stream().collect(collectingAndThen(toList(), Collections::unmodifiableList));
| Parameters | |
|---|---|
downstream |
Collector: a collector |
finisher |
Function: a function to be applied to the final result of the downstream collector |
| Returns | |
|---|---|
Collector<T, A, RR> |
a collector which performs the action of the downstream collector, followed by an additional finishing step |
counting
public static Collector<T, ?, Long> counting ()
Returns a Collector accepting elements of type T that
counts the number of input elements. If no elements are present, the
result is 0.
Implementation Requirements:
- This produces a result equivalent to:
reducing(0L, e -> 1L, Long::sum)
| Returns | |
|---|---|
Collector<T, ?, Long> |
a Collector that counts the input elements |
groupingBy
public static Collector<T, ?, Map<K, List<T>>> groupingBy (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, and returning the results in a Map.
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 Lists 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.
Implementation Requirements:
- This produces a result similar to:
groupingBy(classifier, toList());
Implementation Note:
- The returned
Collectoris not concurrent. For parallel stream pipelines, thecombinerfunction operates by merging the keys from one map into another, which can be an expensive operation. If preservation of the order in which elements appear in the resultingMapcollector is not required, usinggroupingByConcurrent(java.util.function.Function)may offer better parallel performance.
| Parameters | |
|---|---|
classifier |
Function: the classifier function mapping input elements to keys |
| Returns | |
|---|---|
Collector<T, ?, Map<K, List<T>>> |
a Collector implementing the group-by operation |
groupingBy
public static Collector<T, ?, M> groupingBy (Function<? super T, ? extends K> classifier, Supplier<M> mapFactory, Collector<? super T, A, 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. 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())));
Implementation Note:
- The returned
Collectoris not concurrent. For parallel stream pipelines, thecombinerfunction operates by merging the keys from one map into another, which can be an expensive operation. If preservation of the order in which elements are presented to the downstream collector is not required, usinggroupingByConcurrent(java.util.function.Function, java.util.function.Supplier, java.util.stream.Collector)may offer better parallel performance.
| Parameters | |
|---|---|
classifier |
Function: a classifier function mapping input elements to keys |
mapFactory |
Supplier: a function which, when called, produces a new empty
Map of the desired type |
downstream |
Collector: a Collector implementing the downstream reduction |
| Returns | |
|---|---|
Collector<T, ?, M> |
a Collector implementing the cascaded group-by operation |
groupingBy
public static Collector<T, ?, Map<K, D>> groupingBy (Function<? super T, ? extends K> classifier, Collector<? super T, A, 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.
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>.
There are no guarantees on the type, mutability,
serializability, or thread-safety of the Map returned.
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())));
Implementation Note:
- The returned
Collectoris not concurrent. For parallel stream pipelines, thecombinerfunction operates by merging the keys from one map into another, which can be an expensive operation. If preservation of the order in which elements are presented to the downstream collector is not required, usinggroupingByConcurrent(java.util.function.Function, java.util.stream.Collector)may offer better parallel performance.
| Parameters | |
|---|---|
classifier |
Function: a classifier function mapping input elements to keys |
downstream |
Collector: a Collector implementing the downstream reduction |
| Returns | |
|---|---|
Collector<T, ?, Map<K, D>> |
a Collector implementing the cascaded group-by operation |
groupingByConcurrent
public static Collector<T, ?, ConcurrentMap<K, D>> groupingByConcurrent (Function<? super T, ? extends K> classifier, Collector<? super T, A, 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.
This is a Collector.Characteristics#CONCURRENT and
Collector.Characteristics#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,
mapping(Person::getLastName, toSet())));
| Parameters | |
|---|---|
classifier |
Function: a classifier function mapping input elements to keys |
downstream |
Collector: a Collector implementing the downstream reduction |
| Returns | |
|---|---|
Collector<T, ?, ConcurrentMap<K, D>> |
a concurrent, unordered Collector implementing the cascaded group-by operation |
groupingByConcurrent
public static Collector<T, ?, ConcurrentMap<K, List<T>>> groupingByConcurrent (Function<? super T, ? extends K> classifier)
Returns a concurrent Collector implementing a "group by"
operation on input elements of type T, grouping elements
according to a classification function.
This is a Collector.Characteristics#CONCURRENT and
Collector.Characteristics#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 Lists containing the
input elements which map to the associated key under the classification
function.
There are no guarantees on the type, mutability, or serializability
of the Map or List objects returned, or of the
thread-safety of the List objects returned.
Implementation Requirements:
- This produces a result similar to:
groupingByConcurrent(classifier, toList());
| Parameters | |
|---|---|
classifier |
Function: a classifier function mapping input elements to keys |
| Returns | |
|---|---|
Collector<T, ?, ConcurrentMap<K, List<T>>> |
a concurrent, unordered Collector implementing the group-by operation |
groupingByConcurrent
public static Collector<T, ?, M> groupingByConcurrent (Function<? super T, ? extends K> classifier, Supplier<M> mapFactory, Collector<? super T, A, 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. The ConcurrentMap produced by the
Collector is created with the supplied factory function.
This is a Collector.Characteristics#CONCURRENT and
Collector.Characteristics#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())));
| Parameters | |
|---|---|
classifier |
Function: a classifier function mapping input elements to keys |
mapFactory |
Supplier: a function which, when called, produces a new empty
ConcurrentMap of the desired type |
downstream |
Collector: a Collector implementing the downstream reduction |
| Returns | |
|---|---|
Collector<T, ?, M> |
a concurrent, unordered Collector implementing the cascaded group-by operation |
joining
public static Collector<CharSequence, ?, String> joining (CharSequence delimiter, CharSequence prefix, CharSequence suffix)
Returns a Collector that concatenates the input elements,
separated by the specified delimiter, with the specified prefix and
suffix, in encounter order.
| Parameters | |
|---|---|
delimiter |
CharSequence: the delimiter to be used between each element |
prefix |
CharSequence: the sequence of characters to be used at the beginning
of the joined result |
suffix |
CharSequence: the sequence of characters to be used at the end
of the joined result |
| Returns | |
|---|---|
Collector<CharSequence, ?, String> |
A Collector which concatenates CharSequence elements,
separated by the specified delimiter, in encounter order |
joining
public static Collector<CharSequence, ?, String> joining (CharSequence delimiter)
Returns a Collector that concatenates the input elements,
separated by the specified delimiter, in encounter order.
| Parameters | |
|---|---|
delimiter |
CharSequence: the delimiter to be used between each element |
| Returns | |
|---|---|
Collector<CharSequence, ?, String> |
A Collector which concatenates CharSequence elements,
separated by the specified delimiter, in encounter order |
joining
public static Collector<CharSequence, ?, String> joining ()
Returns a Collector that concatenates the input elements into a
String, in encounter order.
| Returns | |
|---|---|
Collector<CharSequence, ?, String> |
a Collector that concatenates the input elements into a
String, in encounter order |
mapping
public static Collector<T, ?, R> mapping (Function<? super T, ? extends U> mapper, Collector<? super U, A, R> downstream)
Adapts a Collector accepting elements of type U to one
accepting elements of type T by applying a mapping function to
each input element before accumulation.
API Note:
- The
mapping()collectors are most useful when used in a multi-level reduction, such as downstream of agroupingByorpartitioningBy. For example, given a stream ofPerson, 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())));
| Parameters | |
|---|---|
mapper |
Function: a function to be applied to the input elements |
downstream |
Collector: a collector which will accept mapped values |
| Returns | |
|---|---|
Collector<T, ?, R> |
a collector which applies the mapping function to the input elements and provides the mapped results to the downstream collector |
maxBy
public static Collector<T, ?, Optional<T>> maxBy (Comparator<? super T> comparator)
Returns a Collector that produces the maximal element according
to a given Comparator, described as an Optional<T>.
Implementation Requirements:
- This produces a result equivalent to:
reducing(BinaryOperator.maxBy(comparator))
| Parameters | |
|---|---|
comparator |
Comparator: a Comparator for comparing elements |
| Returns | |
|---|---|
Collector<T, ?, Optional<T>> |
a Collector that produces the maximal value |
minBy
public static Collector<T, ?, Optional<T>> minBy (Comparator<? super T> comparator)
Returns a Collector that produces the minimal element according
to a given Comparator, described as an Optional<T>.
Implementation Requirements:
- This produces a result equivalent to:
reducing(BinaryOperator.minBy(comparator))
| Parameters | |
|---|---|
comparator |
Comparator: a Comparator for comparing elements |
| Returns | |
|---|---|
Collector<T, ?, Optional<T>> |
a Collector that produces the minimal value |
partitioningBy
public static Collector<T, ?, Map<Boolean, List<T>>> partitioningBy (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>>.
There are no guarantees on the type, mutability,
serializability, or thread-safety of the Map returned.
| Parameters | |
|---|---|
predicate |
Predicate: a predicate used for classifying input elements |
| Returns | |
|---|---|
Collector<T, ?, Map<Boolean, List<T>>> |
a Collector implementing the partitioning operation |
See also:
partitioningBy
public static Collector<T, ?, Map<Boolean, D>> partitioningBy (Predicate<? super T> predicate, Collector<? super T, A, 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.
There are no guarantees on the type, mutability,
serializability, or thread-safety of the Map returned.
| Parameters | |
|---|---|
predicate |
Predicate: a predicate used for classifying input elements |
downstream |
Collector: a Collector implementing the downstream
reduction |
| Returns | |
|---|---|
Collector<T, ?, Map<Boolean, D>> |
a Collector implementing the cascaded partitioning
operation |
See also:
reducing
public static Collector<T, ?, Optional<T>> reducing (BinaryOperator<T> op)
Returns a Collector which performs a reduction of its
input elements under a specified BinaryOperator. The result
is described as an Optional<T>.
API Note:
- The
reducing()collectors are most useful when used in a multi-level reduction, downstream ofgroupingByorpartitioningBy. To perform a simple reduction on a stream, useStream#reduce(BinaryOperator)instead.For example, given a stream of
Person, to calculate tallest person in each city:Comparator<Person> byHeight = Comparator.comparing(Person::getHeight); Map<City, Person> tallestByCity = people.stream().collect(groupingBy(Person::getCity, reducing(BinaryOperator.maxBy(byHeight))));
| Parameters | |
|---|---|
op |
BinaryOperator: a BinaryOperator<T> used to reduce the input elements |
| Returns | |
|---|---|
Collector<T, ?, Optional<T>> |
a Collector which implements the reduction operation |
reducing
public static Collector<T, ?, U> reducing (U identity, Function<? super T, ? extends U> mapper, BinaryOperator<U> op)
Returns a Collector which performs a reduction of its
input elements under a specified mapping function and
BinaryOperator. This is a generalization of
reducing(java.lang.Object, java.util.function.BinaryOperator) which allows a transformation
of the elements before reduction.
API Note:
- The
reducing()collectors are most useful when used in a multi-level reduction, downstream ofgroupingByorpartitioningBy. To perform a simple map-reduce on a stream, useStream#map(Function)andStream#reduce(Object, BinaryOperator)instead.For example, given a stream of
Person, to calculate the longest last name of residents in each city:Comparator<String> byLength = Comparator.comparing(String::length); Map<City, String> longestLastNameByCity = people.stream().collect(groupingBy(Person::getCity, reducing(Person::getLastName, BinaryOperator.maxBy(byLength))));
| Parameters | |
|---|---|
identity |
U: the identity value for the reduction (also, the value
that is returned when there are no input elements) |
mapper |
Function: a mapping function to apply to each input value |
op |
BinaryOperator: a BinaryOperator<U> used to reduce the mapped values |
| Returns | |
|---|---|
Collector<T, ?, U> |
a Collector implementing the map-reduce operation |
reducing
public static Collector<T, ?, T> reducing (T identity, BinaryOperator<T> op)
Returns a Collector which performs a reduction of its
input elements under a specified BinaryOperator using the
provided identity.
API Note:
- The
reducing()collectors are most useful when used in a multi-level reduction, downstream ofgroupingByorpartitioningBy. To perform a simple reduction on a stream, useStream#reduce(Object, BinaryOperator)} instead.
| Parameters | |
|---|---|
identity |
T: the identity value for the reduction (also, the value
that is returned when there are no input elements) |
op |
BinaryOperator: a BinaryOperator<T> used to reduce the input elements |
| Returns | |
|---|---|
Collector<T, ?, T> |
a Collector which implements the reduction operation |
summarizingDouble
public static Collector<T, ?, DoubleSummaryStatistics> summarizingDouble (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.
| Parameters | |
|---|---|
mapper |
ToDoubleFunction: a mapping function to apply to each element |
| Returns | |
|---|---|
Collector<T, ?, DoubleSummaryStatistics> |
a Collector implementing the summary-statistics reduction |
summarizingInt
public static Collector<T, ?, IntSummaryStatistics> summarizingInt (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.
| Parameters | |
|---|---|
mapper |
ToIntFunction: a mapping function to apply to each element |
| Returns | |
|---|---|
Collector<T, ?, IntSummaryStatistics> |
a Collector implementing the summary-statistics reduction |
summarizingLong
public static Collector<T, ?, LongSummaryStatistics> summarizingLong (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.
| Parameters | |
|---|---|
mapper |
ToLongFunction: the mapping function to apply to each element |
| Returns | |
|---|---|
Collector<T, ?, LongSummaryStatistics> |
a Collector implementing the summary-statistics reduction |
summingDouble
public static Collector<T, ?, Double> summingDouble (ToDoubleFunction<? super T> mapper)
Returns a Collector that produces the sum of a double-valued
function applied to the input elements. If no elements are present,
the result is 0.
The sum returned can vary depending upon the order in which
values are recorded, due to accumulated rounding error in
addition of values of differing magnitudes. Values sorted by increasing
absolute magnitude tend to yield more accurate results. If any recorded
value is a NaN or the sum is at any point a NaN then the
sum will be NaN.
| Parameters | |
|---|---|
mapper |
ToDoubleFunction: a function extracting the property to be summed |
| Returns | |
|---|---|
Collector<T, ?, Double> |
a Collector that produces the sum of a derived property |
summingInt
public static Collector<T, ?, Integer> summingInt (ToIntFunction<? super T> mapper)
Returns a Collector that produces the sum of a integer-valued
function applied to the input elements. If no elements are present,
the result is 0.
| Parameters | |
|---|---|
mapper |
ToIntFunction: a function extracting the property to be summed |
| Returns | |
|---|---|
Collector<T, ?, Integer> |
a Collector that produces the sum of a derived property |
summingLong
public static Collector<T, ?, Long> summingLong (ToLongFunction<? super T> mapper)
Returns a Collector that produces the sum of a long-valued
function applied to the input elements. If no elements are present,
the result is 0.
| Parameters | |
|---|---|
mapper |
ToLongFunction: a function extracting the property to be summed |
| Returns | |
|---|---|
Collector<T, ?, Long> |
a Collector that produces the sum of a derived property |
toCollection
public static Collector<T, ?, C> toCollection (Supplier<C> collectionFactory)
Returns a Collector that accumulates the input elements into a
new Collection, in encounter order. The Collection is
created by the provided factory.
| Parameters | |
|---|---|
collectionFactory |
Supplier: a Supplier which returns a new, empty
Collection of the appropriate type |
| Returns | |
|---|---|
Collector<T, ?, C> |
a Collector which collects all the input elements into a
Collection, in encounter order |
toConcurrentMap
public static Collector<T, ?, M> toConcurrentMap (Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier)
Returns a concurrent Collector that accumulates elements into a
ConcurrentMap whose keys and values are the result of applying
the provided 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 Collector.Characteristics#CONCURRENT and
Collector.Characteristics#UNORDERED Collector.
| Parameters | |
|---|---|
keyMapper |
Function: a mapping function to produce keys |
valueMapper |
Function: a mapping function to produce values |
mergeFunction |
BinaryOperator: a merge function, used to resolve collisions between
values associated with the same key, as supplied
to Map#merge(Object, Object, BiFunction) |
mapSupplier |
Supplier: a function which returns a new, empty Map into
which the results will be inserted |
| Returns | |
|---|---|
Collector<T, ?, M> |
a concurrent, unordered 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 function |
toConcurrentMap
public static Collector<T, ?, ConcurrentMap<K, U>> toConcurrentMap (Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction)
Returns a concurrent Collector that accumulates elements into a
ConcurrentMap whose keys and values are the result of applying
the provided 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.
API Note:
- There are multiple ways to deal with collisions between multiple elements
mapping to the same key. The other forms of
toConcurrentMapsimply use a merge function that throws unconditionally, but you can easily write more flexible merge policies. For example, if you have a stream ofPerson, 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 aMapmapping names to a concatenated list of addresses:Map<String, String> phoneBook people.stream().collect(toConcurrentMap(Person::getName, Person::getAddress, (s, a) -> s + ", " + a));This is a
Collector.Characteristics#CONCURRENTandCollector.Characteristics#UNORDEREDCollector.
| Parameters | |
|---|---|
keyMapper |
Function: a mapping function to produce keys |
valueMapper |
Function: a mapping function to produce values |
mergeFunction |
BinaryOperator: a merge function, used to resolve collisions between
values associated with the same key, as supplied
to Map#merge(Object, Object, BiFunction) |
| Returns | |
|---|---|
Collector<T, ?, ConcurrentMap<K, U>> |
a concurrent, unordered 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 function |
toConcurrentMap
public static Collector<T, ?, ConcurrentMap<K, U>> toConcurrentMap (Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)
Returns a concurrent Collector that accumulates elements into a
ConcurrentMap whose keys and values are the result of applying
the provided 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(java.util.function.Function, java.util.function.Function, java.util.function.BinaryOperator) instead.
API Note:
- It is common for either the key or the value to be the input elements.
In this case, the utility method
Function.identity()may be helpful. For example, the following produces aMapmapping students to their grade point average:
And the following produces aMap<Student, Double> studentToGPA students.stream().collect(toMap(Functions.identity(), student -> computeGPA(student)));Mapmapping a unique identifier to students:Map<String, Student> studentIdToStudent students.stream().collect(toConcurrentMap(Student::getId, Functions.identity());This is a
Collector.Characteristics#CONCURRENTandCollector.Characteristics#UNORDEREDCollector.
| Parameters | |
|---|---|
keyMapper |
Function: the mapping function to produce keys |
valueMapper |
Function: the mapping function to produce values |
| Returns | |
|---|---|
Collector<T, ?, ConcurrentMap<K, U>> |
a concurrent, unordered 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 the input elements |
toList
public static Collector<T, ?, List<T>> toList ()
Returns a 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; if more
control over the returned List is required, use toCollection(java.util.function.Supplier).
| Returns | |
|---|---|
Collector<T, ?, List<T>> |
a Collector which collects all the input elements into a
List, in encounter order |
toMap
public static Collector<T, ?, Map<K, U>> toMap (Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction)
Returns a Collector that accumulates elements into a
Map whose keys and values are the result of applying the provided
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.
API Note:
- There are multiple ways to deal with collisions between multiple elements
mapping to the same key. The other forms of
toMapsimply use a merge function that throws unconditionally, but you can easily write more flexible merge policies. For example, if you have a stream ofPerson, 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 aMapmapping names to a concatenated list of addresses:Map<String, String> phoneBook people.stream().collect(toMap(Person::getName, Person::getAddress, (s, a) -> s + ", " + a));
Implementation Note:
- The returned
Collectoris not concurrent. For parallel stream pipelines, thecombinerfunction operates by merging the keys from one map into another, which can be an expensive operation. If it is not required that results are merged into theMapin encounter order, usingtoConcurrentMap(java.util.function.Function, java.util.function.Function, java.util.function.BinaryOperator)may offer better parallel performance.
| Parameters | |
|---|---|
keyMapper |
Function: a mapping function to produce keys |
valueMapper |
Function: a mapping function to produce values |
mergeFunction |
BinaryOperator: a merge function, used to resolve collisions between
values associated with the same key, as supplied
to Map#merge(Object, Object, BiFunction) |
| Returns | |
|---|---|
Collector<T, ?, Map<K, U>> |
a 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 function |
toMap
public static Collector<T, ?, M> toMap (Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier)
Returns a Collector that accumulates elements into a
Map whose keys and values are the result of applying the provided
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.
Implementation Note:
- The returned
Collectoris not concurrent. For parallel stream pipelines, thecombinerfunction operates by merging the keys from one map into another, which can be an expensive operation. If it is not required that results are merged into theMapin encounter order, usingtoConcurrentMap(java.util.function.Function, java.util.function.Function, java.util.function.BinaryOperator, java.util.function.Supplier)may offer better parallel performance.
| Parameters | |
|---|---|
keyMapper |
Function: a mapping function to produce keys |
valueMapper |
Function: a mapping function to produce values |
mergeFunction |
BinaryOperator: a merge function, used to resolve collisions between
values associated with the same key, as supplied
to Map#merge(Object, Object, BiFunction) |
mapSupplier |
Supplier: a function which returns a new, empty Map into
which the results will be inserted |
| Returns | |
|---|---|
Collector<T, ?, M> |
a 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 function |
toMap
public static Collector<T, ?, Map<K, U>> toMap (Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)
Returns a Collector that accumulates elements into a
Map whose keys and values are the result of applying the provided
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(java.util.function.Function, java.util.function.Function, java.util.function.BinaryOperator)
instead.
API Note:
- It is common for either the key or the value to be the input elements.
In this case, the utility method
Function.identity()may be helpful. For example, the following produces aMapmapping students to their grade point average:
And the following produces aMap<Student, Double> studentToGPA students.stream().collect(toMap(Functions.identity(), student -> computeGPA(student)));Mapmapping a unique identifier to students:Map<String, Student> studentIdToStudent students.stream().collect(toMap(Student::getId, Functions.identity());
Implementation Note:
- The returned
Collectoris not concurrent. For parallel stream pipelines, thecombinerfunction operates by merging the keys from one map into another, which can be an expensive operation. If it is not required that results are inserted into theMapin encounter order, usingtoConcurrentMap(java.util.function.Function, java.util.function.Function)may offer better parallel performance.
| Parameters | |
|---|---|
keyMapper |
Function: a mapping function to produce keys |
valueMapper |
Function: a mapping function to produce values |
| Returns | |
|---|---|
Collector<T, ?, Map<K, U>> |
a Collector which collects elements into a Map
whose keys and values are the result of applying mapping functions to
the input elements |
toSet
public static Collector<T, ?, Set<T>> toSet ()
Returns a 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; if more
control over the returned Set is required, use
toCollection(java.util.function.Supplier).
This is an Collector.Characteristics#UNORDERED
Collector.
| Returns | |
|---|---|
Collector<T, ?, Set<T>> |
a Collector which collects all the input elements into a
Set |