Java Collectors

Collectors is a final class that extends Object class. It provides reduction operations, such as accumulating elements into collections, summarizing elements according to various criteria, etc.

Java Collectors class provides various methods to deal with elements

Methods Description
public static Collector averagingDouble(ToDoubleFunction mapper) It 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.
public static Collector reducing(T identity, BinaryOperator op) It returns a Collector which performs a reduction of its input elements under a specified BinaryOperator using the provided identity.
public static Collector<>> reducing(BinaryOperator op) It returns a Collector which performs a reduction of its input elements under a specified BinaryOperator. The result is described as an Optional.
public static Collector reducing(U identity, Function mapper, BinaryOperator op) It returns a Collector 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.
public static Collector<><>>> groupingBy(Function classifier) It 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.
public static Collector<>> groupingBy(Function classifier, Collector downstream) It 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.
public static > Collector groupingBy(Function classifier, Supplier mapFactory, Collector downstream) It 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.
public static Collector<><>>> groupingByConcurrent(Function classifier) It returns a concurrent Collector implementing a "group by" operation on input elements of type T, grouping elements according to a classification function.
public static Collector<>> groupingByConcurrent(Function classifier, Collector downstream) It 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.
public static > Collector groupingByConcurrent(Function classifier, Supplier mapFactory, Collector downstream) It 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.
public static Collector<><>>> partitioningBy(Predicate predicate) It returns a Collector which partitions the input elements according to a Predicate, and organizes them into a Map>. There are no guarantees on the type, mutability, serializability, or thread-safety of the Map returned.
public static Collector<>> partitioningBy(Predicate predicate, Collector downstream) It 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 whose values are the result of the downstream reduction.
public static Collector<>> toMap(Function keyMapper, Function valueMapper) It 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.
public static Collector<>> toMap(Function keyMapper, Function valueMapper, BinaryOperator mergeFunction) It 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.
public static > Collector toMap(Function keyMapper, Function valueMapper, BinaryOperator mergeFunction, Supplier mapSupplier) It 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.
public static Collector<>> toConcurrentMap(Function keyMapper, Function valueMapper) It 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.
public static Collector<>> toConcurrentMap(Function keyMapper, Function valueMapper, BinaryOperator mergeFunction) It 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.
public static > Collector toConcurrentMap(Function keyMapper, Function valueMapper, BinaryOperator mergeFunction, Supplier mapSupplier) It 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.
public static Collector summarizingInt(ToIntFunction mapper) It returns a Collector which applies an int-producing mapping function to each input element, and returns summary statistics for the resulting values.
public static Collector summarizingLong(ToLongFunction mapper) It returns a Collector which applies an long-producing mapping function to each input element, and returns summary statistics for the resulting values.
public static Collector summarizingDouble(ToDoubleFunction mapper) It returns a Collector which applies an double-producing mapping function to each input element, and returns summary statistics for the resulting values.

Java Collectors Example: Fetching data as a List

Output:

[25000.0, 30000.0, 28000.0, 28000.0, 90000.0]

Java Collectors Example: Converting Data as a Set

Output:

[25000.0, 30000.0, 28000.0, 90000.0]

Java Collectors Example: using sum method

Output:

Sum of prices: 201000.0
Sum of id's: 15

Java Collectors Example: Getting Product Average Price

Output:

Average price is: 40200.0

Java Collectors Example: Counting Elements

Output:

Total elements : 5




Contact US

Email:[email protected]

Collectors class
10/30