EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Java 8 Tutorial Java 8 Collectors
Secondary Sidebar
Java Technology Tutorial
  • Java 7, Java 8 and Java 9
    • Java 7 Features
    • What's New in Java 8
    • Java 8 Features
    • How to Install Java 8
    • Java 8 Collections
    • Java 8 Collectors
    • Optional Class in Java 8
    • Java 8 Stream
    • Java 8 Interview Questions
    • Java 9 Features
  • Java servlet
    • What is Servlet
    • Servlet API
    • JAX-RS Jersey
    • Svelte Components
    • Java collection cheat sheet
    • Java collection hierarchy
    • Java collection api
    • Servlet Life Cycle
    • ServletContext
    • Servlet Filter
    • Session in Servlet
    • Session Bean
    • Entity Beans
    • Entity Framework Insert
    • What is JDB
    • Java Servlet Interview Questions
    • Servlet Types
    • Servlet Listener
    • Servlet Interview Questions
  • JSP
    • JSP Page
    • JSP if else
    • JSP property
    • JSP request
    • JSP format
    • What is JSP
    • JSP in Java
    • JSP Life Cycle
    • JSP?Architecture?
    • JSP Objects
    • Custom Tags in JSP
    • JSP Tag Library
    • JSP Directives
    • JSP Scripting Elements
    • JSP foreach
    • JSP Checkbox
    • JSP Include
    • JSP Taglib
    • JSP Scriptlet
    • JSP Declaration
    • Servlet JSP
    • JSP Redirect
    • JSP Forward
    • JSP Filters
    • JSP Formatter
    • JSP Expression
    • JSP File
    • Cookies in JSP
    • JSP Login Page
    • JSP Error Page
    • JSP getParameter
    • JSP Session
    • JSP Alert
    • JSP Implicit Objects
    • JSP Interview Questions
  • EJB
    • What is EJB
    • EJB Architecture
    • EJB Interview Questions
  • Java awt
    • What is AWT in Java
    • Java ActionListener
    • Java AWT Controls
    • Java MouseListener
    • Cron Scheduler in Java
    • JavaMail Api
    • Robot Framework with Java
    • Java Mail Properties
    • Java collection set
    • Java WindowListener
    • Java KeyListener
    • Java ItemListener
    • ScrollBar in Java
  • JSF
    • What is JSF
    • JSF Life Cycle
    • JSF Components
    • JSF Validation
    • JSF Interview Questions
  • JPA
    • What is JPA?
    • Java Persistence API
    • JPA Annotations
    • JAX-RS
    • What is ORM?
    • JPQL
  • JSTL
    • What is JSTL
    • JSTL Tags
    • JSTL replace
  • J2EE
    • What is JEE?
    • J2EE Framework
    • J2EE Architecture
  • Struct
    • What is Struts?
    • Struts Framework
    • Struts Architecture
  • Ant
    • Apache Ant
    • RESTful Services
    • Nutch Apache
    • Applet Life Cycle
    • Apache POI Dependency
  • JDBC
    • JDBC Batch Update
    • JDBC Insert
    • JDBC PreparedStatement
    • JDBC MySQL Driver
    • JFreeChart
    • JDBC Update
    • JDBC template
    • JDBC transaction
    • JDBC Types
    • JDBC Batch Insert
    • JDBC getConnection
    • JDBC connector
    • JDBC datasource
    • JDBC fetch size
    • JDBC resultset
    • JDBC Statement

Related Courses

Java Servlet Training Course

JavaFX Training Course

Jmeter Testing Training Course

Java 8 Collectors

By Shobha ShivakumarShobha Shivakumar

Java 8 Collectors

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,

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • 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,

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,754 ratings)
  • 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 –

  1. Layout in Java
  2. Java Compilers
  3. Merge Sort In Java
  4. Java Runtime class
Popular Course in this category
Java Training (41 Courses, 29 Projects, 4 Quizzes)
  41 Online Courses |  29 Hands-on Projects |  305+ Hours |  Verifiable Certificate of Completion
4.8
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more