Introduction to Java 8 Collectors
A final class that is extended by the object class are collectors to provide operations on reduction like an accumulation of elements into the collection, based on different criteria elements are summarized, etc. and the elements are dealt by using the methods available in Java collectors class and this class is a member of a utility class java.util.stream package which is consisting of many static methods.
Syntax to Declare Java Collectors is as follows:
Interface collector <T,A,R>
Where,
- T indicates the input elements type on which the reduction operation is to be performed.
- A indicates the reduction operations’ accumulation type and it is mutable.
- R indicates the reduction type’s result.
Syntax to import java collectors is as follows:
import static java.util.stream.Collectors.*;
There are different types of collectors and the syntax to import the collectors of different types is as follows:
import static java.util.stream.Collectors.toList.
import static java.util.stream.Collectors.toMap.
import static java.util.stream.Collectors.toSet.
Working of Collectors in Java 8
The java.util.stream.collectors class provides thirty-seven different collectors which are further divided into three groups namely:
- A single value or collection type is reduced or summarized.
- Joining() method can be used to join the strings, new collections can be created using toset(), the new features like numeric streams summary can be leveraged using summarizingInt().
- Grouping: GroupingBy() method can be used in three different ways and parallel or concurrent processing using another three.
- Partitioning.
Two methods like partitionBy() are available. We can create our own collectors if we need unique handling. There are no restrictions on the provided collectors.
The interface collector <T, A, R> is implemented by every collector where,
- T indicates the input elements type on which the reduction operation is to be performed.
- A indicates the reduction operations’ accumulation type and it is mutable.
- R indicates the reduction type’s result.
Methods of Java 8 Collectors
The methods supported by the collector are:
- Supplier(): New instances of the objects of the accumulator are created using supplier<A> provided by the supplier() method.
- Combiner(): Multiple objects of the accumulator are created by the collector when working with parallel processing of stream and these multiple objects can be merged by using the functionality provided by the combiner() method.
- Finisher(): The transformation of the accumulator object to return type R while finishing the collection process is done by this finisher() method.
- Characteristics(): The characteristics of the collector are described using this characteristic () method.
Characteristics of Collectors
The implementation of the reduction operation can be optimized by using the characteristics of the collector. The three characteristics of combination possible is as follows:
1. Collector.characteristics.CONCURRENT
This is used for the indication of parallel or concurrent processing supported by the accumulator objects.
2. Collector.characteristics.IDENTITY_FINISH
The finisher function is indicated as the identity function so that the accumulator is cast directly in the result type.
3. Collector.characteristics.UNORDERED
This is used to indicate the order of the elements in the stream which is not preserved necessarily.
There are several methods supported by Java 8 Collectors, they are:
public static<T> collector <T,?, Double> averagingDouble( ToDoubleFunction<? Super T>mapper)
A double valued function whose arithmetic mean is applied to the input elements is produced by the collector using this method. The result is zero if there are no elements present.
public static <T> Collector <T, ?, T> reducing(T identity, BinaryOperator<T> op)
The reduction of input elements is performed using a collector which is returned using this method and it comes under a specified binary operator using the given identity.
public static <T> Collector <T, ?, Optional <T>> reducing( BinaryOperator< T> op)
The reduction of the input elements is performed by the collector returned by using this method under a certain binary operator. An optional<T> describes the result.
public static <T,U> Collector <T,?,U> reducing(U identity, Function<? Super T,? extends U> mapper, Binary operator<U> op)
The reduction of input elements is performed using a collector which is returned by this method under a certain binary operator and mapping function which is nothing but a generalization of reducing(Object, Binaryoperator) allowing the transformation of elements before reduction.
public static <T,K> collector<T, ?, Map<K, list<T> >> groupingby( function<? Super T,? extends K> classifier)
A collector is returned which implements an operation of a group by on input elements which are of type t, the elements are grouped based on the classification function and the results are returned in a map.
public static <T,K,A, D> Collector< T,?,Map< K,D> > groupingBy( Function<? Super T,? extends K> classifier, Collector<? Super T,A, D> downstream)
The cascaded group by the operation is performed on input elements of type T, and the elements are grouped based on a classification function which is implemented by a collector returned using this method and reduction operation is performed on the values with a specific key associated with the given values using the downstream collector.
public static <T,K,D, A, MextendsMap< K,D> > Collector< T,?,M> groupingby( Function<? Super T,? extends K> classifier, supplier< M> mapfactory, collector<? Super T,A,D> downstream)
The cascaded group by the operation is performed on input elements of type T, and the elements are grouped based on a classification function which is implemented by a collector returned using this method and reduction operation is performed on the values with a specific key associated with the given values using the downstream collector. The supplied factory function is used to create the map produced by the collector.
public static< T,K> Collector< T,?,ConcurrentMap< K,List< T>> > groupingbyConcurrent( Function<? Super T,? extends K> classifier)
A collector is returned which implements an operation of a group by on input elements which are of type t, the elements are grouped based on the classification function.
public static< T,K,A,D> Collector<T,?, ConcurrentMap< K,D> > groupingbyconcurrent( Function<? Super T,? extends K> Classifier, Collector<? Super T,A,D> downstream)
The cascaded group by the operation is performed on input elements of type T, and the elements are grouped based on a classification function which is implemented by a collector returned using this method and reduction operation is performed on the values with a specific key associated with the given values using downstream collector.
public 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)
A concurrent collector is returned which implements on input elements the cascaded group by operation, the elements are grouped according to a classification function and a reduction operation is performed on the values using downstream collector which is associated with a given key. The supplied factory function creates the ConcurrentMap which is produced by the collector.
public static< T> collector< T,?,Map<Boolean,List< T>> > partitioningBy( Predicate<? Super T> predicate)
A collector is returned using which the partitioning of input elements is done as per the predicate and then organized into Map< Boolean, List< T>>. Type, mutability, serializabilty, thread safety of the map cannot be guaranteed.
public static<T,D,A> Collector<T,?,Map< Boolean,D> > partitioningBy(Predicate<? Super T> predicate, Collector<? Super T,A,D> downstream)
A collector is returned using which the partitioning of input elements is done as per the predicate, each partition’s values are reduced according to another collector, and then organized into a Map<Boolean, D> which the results of reduction of downstream.
public static <T,K,U> Collector<T,?,Map<K,U>> toMap(Function<? Super T,? extends K> keyMapper, Function<? Super T,? extends U> valueMapper)
A collector is returned to accumulate the elements into a Map and applying the mapping functions to input elements results in keys and values.
public 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)
A collector is returned to accumulate the elements into a Map and applying the mapping functions to input elements results in keys and values.
public static< T,K, U,MextendsMap< K,U> > Collector< T,?, M> toMap( Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator< U> mergeFunction, Supplier<M> mapSupplier)
A collector is returned to accumulate the elements into a Map and applying the mapping functions to input elements results in keys and values.
public static< T,K,U> Collector<T,?, ConcurrentMap< K,U> > toConcurrentMap( Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper)
A concurrent collector is returned to accumulate the elements into a ConcurrentMap and applying the mapping functions to input elements results in keys and values.
public 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)
A concurrent collector is returned to accumulate the elements into a ConcurrentMap and applying the mapping functions to input elements results in keys and values.
public 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)
A concurrent collector is returned to accumulate the elements into a ConcurrentMap and applying the mapping functions to input elements results in keys and values.
public static< T>Collector<T,?, IntSummaryStatistics> summarizingInt( ToIntFunction<? super T> mapper)
An int producing mapping function is applied to each of the input element by a collector which is returned, and the summary statistics are returned for the resulting values.
public static < T>Collector<T,?, LongSummaryStatistics> summarizingLong( ToLongFunction<? super T> mapper)
A long producing mapping function is applied to each of the input element by a collector which is returned, and the summary statistics are returned for the resulting values.
public static< T>Collector<T,?, DoubleSummaryStatistics> summarizingDouble( ToDoubleFunction<? super T> mapper)
A double producing mapping function is applied to each of the input element by a collector which is returned, and the summary statistics are returned for the resulting values.
Conclusion
In this tutorial, we understand the concept of collectors in Java version 8 through definition. And then understand the syntax to declare the collectors and syntax to import the collectors. Also, we understand the working of collectors in Java 8 and several methods implemented using collectors.
Recommended Articles
This is a guide to Java 8 Collectors. Here we discuss the Introduction to Java 8 Collectors and its different Methods along with Characteristics. You can also go through our other suggested articles to learn more –