Stream Group by operation used for summing, averaging based on specified group by cause. Here is different -different example of group by operation.

Example 1: Stream Group By field and Collect List

java.util.List<Employee> employees = Employee.getEmployee();
        java.util.Map<String, java.util.List<Employee>> employess = employees.stream()
                .collect(Collectors.groupingBy(Employee::getDesignation));

        employess.forEach((name, employeeList) -> {
            System.out.println("Group Name: " + name);
            employeeList.forEach(System.out::println);
        });

Output:

Group Name: Sr. Developer
2 : Joy : Male : Sr. Developer
Group Name: Developer
1 : Bob : Male : Developer
4 : Bat : Male : Developer
5 : Jolly : Female : Developer
6 : Bobby : Female : Developer
Group Name: CEO
3 : John : Male : CEO

Example 2: Stream Group By Field and Collect Count

java.util.List<Employee> employees = Employee.getEmployee();
        java.util.Map<String, Long> employess = employees.stream()
                .collect(Collectors.groupingBy(employee->employee.getDesignation(),Collectors.counting()));

        employess.forEach((name, total) -> {
            System.out.println(name +" - "+ total);
        });

Output:

Sr. Developer - 1
Developer - 4
CEO - 1

Example 3: Stream Group By Field and Collect Only Single field

java.util.List<Employee> employees = Employee.getEmployee();
       java.util.Map<String, java.util.List<String>> employess = employees.stream()
                                                                     .collect(Collectors.groupingBy(Employee::getDesignation, Collectors.mapping(Employee::getName, Collectors.toList())));

       employess.forEach((name, employeeList) -> {
           System.out.println("Group Name: " + name);
           employeeList.forEach(System.out::println);
           System.out.println("---------------------------------------------");
       });

Output:

Group Name: Sr. Developer
Joy
---------------------------------------------
Group Name: Developer
Bob
Bat
Jolly
Bobby
---------------------------------------------
Group Name: CEO
John
---------------------------------------------

Example 4: Stream Group By multiple fields

java.util.List<Employee> employees = Employee.getEmployee();
       java.util.Map<String, java.util.Map<String,java.util.List<Employee>>> employessGroup= employees.stream()
               .collect(Collectors.groupingBy(employee->employee.getDesignation(),Collectors.groupingBy(employee-> employee.getGender())));

       employessGroup.forEach((designation, designationMap) -> {
           System.out.println("Designation --> "+designation);
           designationMap.forEach((gender,employeeList)->{
               System.out.println("Gender -------> "+gender);
               employeeList.forEach(System.out::println);
           });
           System.out.println();
       });

 Output:

Designation --> Sr. Developer
Gender -------> Male
2 : Joy : Male : Sr. Developer

Designation --> Developer
Gender -------> Female
5 : Jolly : Female : Developer
6 : Bobby : Female : Developer
Gender -------> Male
1 : Bob : Male : Developer
4 : Bat : Male : Developer

Designation --> CEO
Gender -------> Male
3 : John : Male : CEO

 

 Employee.java

package com.javadeveloperzone;

import java.util.ArrayList;

/**
 * Created by JavaDeveloperZone on 29-04-2017.
 */
public class Employee {
    private int no;
    private String name;
    private String designation;
    private String gender;

    public Employee(int no, String name, String designation, String gender) {
        this.no = no;
        this.name = name;
        this.designation = designation;
        this.gender = gender;
    }

    public int getNo() {
        return no;
    }
    public void setNo(int no) {
        this.no = no;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDesignation() {
        return designation;
    }
    public void setDesignation(String designation) {
        this.designation = designation;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    @Override
    public String toString() {
        return this.no + " : " + this.name + " : " + gender + " : " + designation;
    }
    public static java.util.List<Employee> getEmployee() {
        java.util.List<Employee> employees = new ArrayList<>();
        employees.add(new Employee(1, "Bob", "Developer", "Male"));
        employees.add(new Employee(2, "Joy", "Sr. Developer", "Male"));
        employees.add(new Employee(3, "John", "CEO", "Male"));
        employees.add(new Employee(4, "Bat", "Developer", "Male"));
        employees.add(new Employee(5, "Jolly", "Developer", "Female"));
        employees.add(new Employee(6, "Bobby", "Developer", "Female"));
        return employees;
    }
}

Ref:

https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html#groupingBy-java.util.function.Function-java.util.stream.Collector-

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

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