Java 8 Collectors examples

Java 8 Collectors
In this post,  we are going to see java 8 Collectors examples. You can do various operations such as average, count, groupby, sort the list with the help of Collectors. I am not providing theory here, I think you will learn better with the help of examples.
Examples:

Counting:

Counting is used to count number of elements in the stream.It returns Collector instance which can be accepted by collect method.

When you run above code, you will get below output:

AveragingInt :

AveragingInt is used to find average of stream elements as int datatype. It returns Collector instance which can be accepted by collect method.

When you run above code, you will get below output:

30.0
1100.0

Lets understand how did you get 30 for case 1 :
(10+20+30+40+50/5)= 150/5 =30.0 
Now you must wondering how did we get 1100 for 2nd case:
(10*10 + 20*20 + 30*30 + 40*40 + 50*50)/5=5500/5 = 1100.0 
If you want to understand more about v-> v*v , you can go through Java 8 lambda expressions Similarly we have different function for different data types such as AveragingDouble, AveragingLong.

joining

Joining method is used to concatenate with delimiter, you can also pass prefix and suffix.

When you run above code, you will get below output:

String with hyphen : Arpit-John-Martin
String with hyphen , suffix and prefix : ==Arpit-John-Martin==

summingint:

summingInt is used to find sum of stream elements as int datatype. It returns Collector instance which can be accepted by collect method.

When you run above code, you will get below output:

150
5500

Similarly we have different function for different data types such as summingDouble, summingLong.

collectingAndThen:

collectingAndThen: is used to get a Collector instance and perform finishing function on top of it.

When you run above code, you will get below output:

75

Reference: 
https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html


import_contacts

You may also like:

Related Posts

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    In this post, we will see how to convert Date to LocalDate in java. Sometimes, we may need to convert Date to new Java 8 APIs and vice versa. There are multiple ways to convert Date to LocalDate in java. Read also: Convert LocalDate to Date in java Using toInstant() method of Date class You […]

  • Convert LocalDate to Date in java
    11 January

    Java LocalDate to Date

    In this post, we will see how to convert LocalDate to Date. Java 8 has introduced a lot of new APIs for Date and time. There can be many ways to convert Java LocalDateTime to date. Using Instant object You can convert LocalDate to Date using Instant object which we can from Zone. Here is […]

  • Convert Stream to List in java
    31 December

    Java Stream to List

    In this post, we will see how to convert Stream to List in java. There are multiple ways to convert Stream to List in java. Using Collectors.toList() You can pass Collectors.toList() to Stream.collect() method to convert Stream to List in java. Stream’s collect method performs mutable reduction operation on elements of Stream and Collectors.toList() provides […]

  • Convert LocalDateTime to Timestamp in java
    18 November

    Convert LocalDateTime to Timestamp in Java

    In this post, we will how to convert LocalDateTime to Timestamp . Before learning how to convert localdatetime to timestamp, let us learn about LocalDateTime and Timestamp, and understand the importance of this conversion. LocalDateTime LocalDateTime was introcuded in Java 8. LocalDateTime can be imported time package: import java.time.LocalDateTime; LocalDateTime is an immutable object used […]

  • 28 August

    A In-Depth guide to Java 8 Stream API

    In this post, we will see an in-depth overview of Java 8 streams with a lot of examples and exercises. Introduction You may think that Stream must be similar to InputStream or OutputStream, but that’s not the case. A Stream represents a sequence of elements supporting sequential and parallel aggregate operations. Stream does not store […]

  • Java Stream List to Map
    26 April

    Java Stream List to Map

    In this post, we will see how to convert List to Map using Stream in java 8. Collectors’s toMap() can be used with Stream to convert List to Map in java. Consider a class Named Movie which have 3 fields – id, name and genre [crayon-603799d43ab04577877783/] Create a list of movies and convert with to […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.