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));
Modifier and Type | Class and Description |
---|---|
pack-priv static record | Collectors.
the type of elements to be collected T, A, the type of the result R>Simple implementation class for |
private static class |
Modifier and Type | Field and Description |
---|---|
pack-priv static final Set | |
pack-priv static final Set | |
pack-priv static final Set | |
pack-priv static final Set | |
pack-priv static final Set | |
pack-priv static final Set |
Access | Constructor and Description |
---|---|
private |
Modifier and Type | Method and Description |
---|---|
public static < the type of the input elements T> Collector | Returns: aCollector that produces the arithmetic mean of a
derived propertya function extracting the property to be averaged mapper)Returns a |
public static < the type of the input elements T> Collector | Returns: aCollector that produces the arithmetic mean of a
derived propertya function extracting the property to be averaged mapper)Returns a |
public static < the type of the input elements T> Collector | Returns: aCollector that produces the arithmetic mean of a
derived propertya function extracting the property to be averaged mapper)Returns a |
private static <T> Supplier | |
private static <I, R> Function | |
public static < the type of the input elements T, intermediate accumulation type of the downstream collector A, result type of the downstream collector R, result type of the resulting collector RR> Collector | Returns: a collector which performs the action of the downstream collector, followed by an additional finishing stepa collector downstream, Function<R, RR> a function to be applied to the final result of the downstream collector finisher)Adapts a |
pack-priv static double | computeFinalSum(double[] summands)
If the compensated sum is spuriously NaN from accumulating one or more same-signed infinite values, return the correctly-signed infinity stored in the simple sum. |
public static < the type of the input elements T> Collector | Returns: aCollector that counts the input elementsReturns a |
private static IllegalStateException | duplicateKeyException(Object
the duplicate key k, Object 1st value to be accumulated/merged u, Object 2nd value to be accumulated/merged v)Construct an |
public static < the type of the input elements T, intermediate accumulation type of the downstream collector A, result type of collector R> Collector | Returns: a collector which applies the predicate to the input elements and provides matching elements to the downstream collectora predicate to be applied to the input elements predicate, Collector<? super T, A, R> a collector which will accept values that match the
predicate downstream)Adapts a |
public static < the type of the input elements T, type of elements accepted by downstream collector U, intermediate accumulation type of the downstream collector A, result type of collector R> Collector | Returns: a collector which applies the mapping function to the input elements and provides the flat mapped results to the downstream collectora function to be applied to the input elements, which
returns a stream of results mapper, Collector<? super U, A, R> a collector which will receive the elements of the
stream returned by mapper downstream)Adapts a |
public static < the type of the input elements T, the type of the keys K> Collector | Returns: aCollector implementing the group-by operationthe classifier function mapping input elements to keys classifier)Returns a |
public static < the type of the input elements T, the type of the keys K, the intermediate accumulation type of the downstream collector A, the result type of the downstream reduction D> Collector | Returns: aCollector implementing the cascaded group-by operationa classifier function mapping input elements to keys classifier, Collector<? super T, A, D> a downstream)Collector implementing the downstream reductionReturns a |
public static < the type of the input elements T, the type of the keys K, the result type of the downstream reduction D, the intermediate accumulation type of the downstream collector A, the type of the resulting M extends MapMap | Returns: aCollector implementing the cascaded group-by operationa classifier function mapping input elements to keys classifier, Supplier<M> a supplier providing a new empty mapFactory, Collector<? super T, A, D> Map
into which the results will be inserteda downstream)Collector implementing the downstream reductionReturns a |
public static < the type of the input elements T, the type of the keys K> Collector | Returns: a concurrent, unorderedCollector implementing the group-by operationa classifier function mapping input elements to keys classifier)Returns a concurrent |
public static < the type of the input elements T, the type of the keys K, the intermediate accumulation type of the downstream collector A, the result type of the downstream reduction D> Collector | Returns: a concurrent, unorderedCollector implementing the cascaded group-by operationa classifier function mapping input elements to keys classifier, Collector<? super T, A, D> a downstream)Collector implementing the downstream reductionReturns a concurrent |
public static < the type of the input elements T, the type of the keys K, the intermediate accumulation type of the downstream collector A, the result type of the downstream reduction D, the type of the resulting M extends ConcurrentMapConcurrentMap | Returns: a concurrent, unorderedCollector implementing the cascaded group-by operationa classifier function mapping input elements to keys classifier, Supplier<M> a supplier providing a new empty mapFactory, Collector<? super T, A, D> ConcurrentMap
into which the results will be inserteda downstream)Collector implementing the downstream reductionReturns a concurrent |
public static Collector | Returns: aCollector that concatenates the input elements into a
String , in encounter orderReturns a |
public static Collector | Returns: ACollector which concatenates CharSequence elements,
separated by the specified delimiter, in encounter orderthe delimiter to be used between each element delimiter)Returns a |
public static Collector | Returns: ACollector which concatenates CharSequence elements,
separated by the specified delimiter, in encounter orderthe delimiter to be used between each element delimiter, CharSequence the sequence of characters to be used at the beginning
of the joined result prefix, CharSequence the sequence of characters to be used at the end
of the joined result suffix)Returns a |
private static < type of the map keys K, type of the map values V, type of the map M extends Map | Returns: a merge function for two mapsA merge function suitable for
mergeFunction)Map.
|
public static < the type of the input elements T, type of elements accepted by downstream collector U, intermediate accumulation type of the downstream collector A, result type of collector R> Collector | Returns: a collector which applies the mapping function to the input elements and provides the mapped results to the downstream collectora function to be applied to the input elements mapper, Collector<? super U, A, R> a collector which will accept mapped values downstream)Adapts a |
public static < the type of the input elements T> Collector | Returns: aCollector that produces the maximal valuea comparator)Comparator for comparing elementsReturns a |
public static < the type of the input elements T> Collector | Returns: aCollector that produces the minimal valuea comparator)Comparator for comparing elementsReturns a |
public static < the type of the input elements T> Collector | Returns: aCollector implementing the partitioning operationa predicate used for classifying input elements predicate)Returns a |
public static < the type of the input elements T, the result type of the downstream reduction D, the intermediate accumulation type of the downstream collector A> Collector | Returns: aCollector implementing the cascaded partitioning
operationa predicate used for classifying input elements predicate, Collector<? super T, A, D> a downstream)Collector implementing the downstream
reductionReturns a |
public static < element type for the input and output of the reduction T> Collector | Returns: aCollector which implements the reduction operationthe identity value for the reduction (also, the value
that is returned when there are no input elements) identity, BinaryOperator<T> a op)BinaryOperator<T> used to reduce the input elementsReturns a |
public static < element type for the input and output of the reduction T> Collector | Returns: aCollector which implements the reduction operationa op)BinaryOperator<T> used to reduce the input elementsReturns a |
public static < the type of the input elements T, the type of the mapped values U> Collector | Returns: aCollector implementing the map-reduce operationthe identity value for the reduction (also, the value
that is returned when there are no input elements) identity, Function<? super T, ? extends U> a mapping function to apply to each input value mapper, BinaryOperator<U> a op)BinaryOperator<U> used to reduce the mapped valuesReturns a |
public static < the type of the input elements T> Collector | Returns: aCollector implementing the summary-statistics reductiona mapping function to apply to each element mapper)Returns a |
public static < the type of the input elements T> Collector | Returns: aCollector implementing the summary-statistics reductiona mapping function to apply to each element mapper)Returns a |
public static < the type of the input elements T> Collector | Returns: aCollector implementing the summary-statistics reductionthe mapping function to apply to each element mapper)Returns a |
public static < the type of the input elements T> Collector | Returns: aCollector that produces the sum of a derived propertya function extracting the property to be summed mapper)Returns a |
public static < the type of the input elements T> Collector | Returns: aCollector that produces the sum of a derived propertya function extracting the property to be summed mapper)Returns a |
public static < the type of the input elements T> Collector | Returns: aCollector that produces the sum of a derived propertya function extracting the property to be summed mapper)Returns a |
pack-priv static double[] | sumWithCompensation(double[]
the high-order and low-order words of the intermediate sum intermediateSum, double the name value to be included in the running sum value)Incorporate a new double value using Kahan summation / compensation summation. |
public static < the type of the input elements T, the result type of the first collector R1, the result type of the second collector R2, the final result type R> Collector | Returns: aCollector which aggregates the results of two supplied collectors.the first downstream collector downstream1, Collector<? super T, ?, R2> the second downstream collector downstream2, BiFunction<? super R1, ? super R2, R> the function which merges two results into the single one merger)Returns a |
private static <T, A1, A2, R1, R2, R> Collector | teeing0(Collector<? super T, A1, R1> downstream1, Collector<? super T, A2, R2> downstream2, BiFunction<? super R1, ? super R2, R> merger)
|
public static < the type of the input elements T, the type of the resulting C extends CollectionCollection | Returns: aCollector which collects all the input elements into a
Collection , in encounter ordera supplier providing a new empty collectionFactory)Collection
into which the results will be insertedReturns a |
public static < the type of the input elements T, the output type of the key mapping function K, the output type of the value mapping function U> Collector | Returns: a concurrent, unorderedCollector 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 elementsthe mapping function to produce keys keyMapper, Function<? super T, ? extends U> the mapping function to produce values valueMapper)Returns a concurrent |
public static < the type of the input elements T, the output type of the key mapping function K, the output type of the value mapping function U> Collector | Returns: a concurrent, unorderedCollector 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 functiona mapping function to produce keys keyMapper, Function<? super T, ? extends U> a mapping function to produce values valueMapper, BinaryOperator<U> a merge function, used to resolve collisions between
values associated with the same key, as supplied
to mergeFunction)Map#merge(Object, Object, BiFunction) Returns a concurrent |
public static < the type of the input elements T, the output type of the key mapping function K, the output type of the value mapping function U, the type of the resulting M extends ConcurrentMapConcurrentMap | Returns: a concurrent, unorderedCollector 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 functiona mapping function to produce keys keyMapper, Function<? super T, ? extends U> a mapping function to produce values valueMapper, BinaryOperator<U> a merge function, used to resolve collisions between
values associated with the same key, as supplied
to mergeFunction, Supplier<M> Map#merge(Object, Object, BiFunction) a supplier providing a new empty mapFactory)ConcurrentMap
into which the results will be insertedReturns a concurrent |
public static < the type of the input elements T> Collector | Returns: aCollector which collects all the input elements into a
List , in encounter orderReturns a |
public static < the type of the input elements T, the output type of the key mapping function K, the output type of the value mapping function U> Collector | Returns: aCollector which collects elements into a Map
whose keys and values are the result of applying mapping functions to
the input elementsa mapping function to produce keys keyMapper, Function<? super T, ? extends U> a mapping function to produce values valueMapper)Returns a |
public static < the type of the input elements T, the output type of the key mapping function K, the output type of the value mapping function U> Collector | Returns: aCollector 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 functiona mapping function to produce keys keyMapper, Function<? super T, ? extends U> a mapping function to produce values valueMapper, BinaryOperator<U> a merge function, used to resolve collisions between
values associated with the same key, as supplied
to mergeFunction)Map#merge(Object, Object, BiFunction) Returns a |
public static < the type of the input elements T, the output type of the key mapping function K, the output type of the value mapping function U, the type of the resulting M extends MapMap | Returns: aCollector 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 functiona mapping function to produce keys keyMapper, Function<? super T, ? extends U> a mapping function to produce values valueMapper, BinaryOperator<U> a merge function, used to resolve collisions between
values associated with the same key, as supplied
to mergeFunction, Supplier<M> Map#merge(Object, Object, BiFunction) a supplier providing a new empty mapFactory)Map
into which the results will be insertedReturns a |
public static < the type of the input elements T> Collector | Returns: aCollector which collects all the input elements into a
Set Returns a |
public static < the type of the input elements T> Collector | Returns: aCollector that accumulates the input elements into an
unmodifiable List in encounter orderReturns a |
public static < the type of the input elements T, the output type of the key mapping function K, the output type of the value mapping function U> Collector | Returns: aCollector that accumulates the input elements into an
unmodifiable Map, whose keys and values
are the result of applying the provided mapping functions to the input elementsa mapping function to produce keys, must be non-null keyMapper, Function<? super T, ? extends U> a mapping function to produce values, must be non-null valueMapper)Returns a |
public static < the type of the input elements T, the output type of the key mapping function K, the output type of the value mapping function U> Collector | Returns: aCollector that accumulates the input elements into an
unmodifiable Map, whose keys and values
are the result of applying the provided mapping functions to the input elementsa mapping function to produce keys, must be non-null keyMapper, Function<? super T, ? extends U> a mapping function to produce values, must be non-null valueMapper, BinaryOperator<U> a merge function, used to resolve collisions between
values associated with the same key, as supplied
to mergeFunction)Map#merge(Object, Object, BiFunction) ,
must be non-nullReturns a |
public static < the type of the input elements T> Collector | toUnmodifiableSet()
Returns a |
private static < type of elements T, type of map keys K, type of map values V> BiConsumer | Returns: an accumulating consumera function that maps an element into a key keyMapper, Function<? super T, ? extends V> a function that maps an element into a value valueMapper)
|
private static < type of the map keys K, type of the map values V, type of the map M extends Map | Returns: a merge function for two maps
|