Java 8 groupingBy Example | Collectors.groupingBy() Java 8 online Tutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, I am going to show you how to group a list of objects in Java 8.
groupingBy() is a static method available in java.util.stream.Collectors. Which is used to grouping the objects on the basis of any key and then it returns a Collector.
The above groupingBy() method, grouping the input elements(T) according to the classification function and returning the Map as a Collector.
In this example, we are going to group the Employee objects according to the department ids.
public class Employee {
    private int id;
    private int department;
    private String name;

    public Employee(int id, int department, String name) {
        super();
        this.id = id;
        this.department = department;
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public int getDepartment() {
        return department;
    }
    public String getName() {
        return name;
    }

}
Grouping Emploeyee objects using Java8
package com.onlinetutorialspoint.java8;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Java8_GroupingBy {

 public static void main(String[] args) {

  List < Employee > employees = Arrays.asList(
   new Employee(1, 10, "Chandra"), new Employee(1, 20, "Rajesh"),
   new Employee(1, 30, "Rahul"), new Employee(1, 20, "Ramana"));

  Map < Integer, List < Employee >> byDept = employees.stream().collect(
   Collectors.groupingBy(Employee::getDepartment));

  byDept.forEach((k, v) -> System.out.println("DeptId:" +k +"   " +
   ((List < Employee > ) v).stream().map(m -> m.getName())
   .collect(Collectors.joining(","))));
 }

}
Output:
DeptId:20  Rajesh,Ramana
DeptId:10  Chandra
DeptId:30  Rahul
Happy Learning 🙂

How to convert List to Map in Java 8
How to calculate Employees Salaries Java 8 summingInt
Resolve NullPointerException in Collectors.toMap
Java 8 Stream Filter Example with Objects
How to get Stream count in Java 8
How to Merge Streams in Java 8
Spring Collection Dependency List Example
Parallel Streams in Java 8 Example
How to Convert Iterable to Stream Java 8
How to Filter null values from Java8 Stream
Java 8 How to convert Stream to List
How to Filter a Map in Java 8
User defined sorting with Java 8 Comparator
Java 8 walk How to Read all files in a folder
Java 8 how to remove duplicates from list

How to convert List to Map in Java 8
How to calculate Employees Salaries Java 8 summingInt
Resolve NullPointerException in Collectors.toMap
Java 8 Stream Filter Example with Objects
How to get Stream count in Java 8
How to Merge Streams in Java 8
Spring Collection Dependency List Example
Parallel Streams in Java 8 Example
How to Convert Iterable to Stream Java 8
How to Filter null values from Java8 Stream
Java 8 How to convert Stream to List
How to Filter a Map in Java 8
User defined sorting with Java 8 Comparator
Java 8 walk How to Read all files in a folder
Java 8 how to remove duplicates from list






santosh miriyala
April 10, 2017 at 5:52 am - Reply 

Hi chandrashekhar, its a nice article, Can you please prepare few posts I wish to perform the same grouping mechanism in java 7 or below versions.









Milana Travis
October 25, 2017 at 12:17 pm - Reply 

Thank you very much for your blog.
I enjoyed reading this article.










santosh miriyala
April 10, 2017 at 5:52 am - Reply 

Hi chandrashekhar, its a nice article, Can you please prepare few posts I wish to perform the same grouping mechanism in java 7 or below versions.




Hi chandrashekhar, its a nice article, Can you please prepare few posts I wish to perform the same grouping mechanism in java 7 or below versions.





Milana Travis
October 25, 2017 at 12:17 pm - Reply 

Thank you very much for your blog.
I enjoyed reading this article.




Thank you very much for your blog.
I enjoyed reading this article.
 


Δ
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String