Java 8 : Group by Operation With Example

Let’s look into the group by operation provided by Java 8 for grouping elements together. It is similar to the one provided by SQL.

import java.util.*;
import java.util.stream.*;
import java.util.function.Function;

public class MyClass {
    public static void main(String args[]) {
      List<String> items = Arrays.asList("apple","guava","banana","apple","guava","orange","banana","apple");
      Map<String,Long> result = items.stream().collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));
      
      System.out.println("The count of all fruits are as follows : "+result);
      
    }
}

So if you see above, the fruits were grouped by their respective counts.

Let’s group by using Objects.

Here we are grouping by the student name to find his total points scored in all the rounds.

import java.util.*;
import java.util.stream.*;

class Student {

    private String name;
    private int round;
    private int points;
    
    public Student(String name, int round, int points) {
        super();
        this.name = name;
        this.round = round;
        this.points = points;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getRound() {
        return round;
    }
    public void setRound(int round) {
        this.round = round;
    }
    public int getPoints() {
        return points;
    }
    public void setPoints(int points) {
        this.points = points;
    }
}

public class Java8GroupByExample {
    public static void main(String args[]) {
      
      List<Student> studList = Java8GroupByExample.createStudList();
      
      Map<String,Integer> studPoints = 
      studList.stream().collect(Collectors.groupingBy(Student::getName,Collectors.summingInt(Student::getPoints)));
      System.out.println("Grouped by student total points : "+studPoints);
      
    }
    
    public static List<Student> createStudList()
    {
        List<Student> studList=new ArrayList<Student>();
    
        Student s1=new Student("Nathan",1,500);    
        Student s2=new Student("Nathan",2,1200);
        Student s3=new Student("Nathan",3,1500);
        Student s4=new Student("Chris",1,1000);
        Student s5=new Student("Chris",2,2000);
        Student s6=new Student("Chris",3,200);    
        Student s7=new Student("David",1,100);
        Student s8=new Student("David",2,1000);
        Student s9=new Student("David",3,500);
        Student s10=new Student("James",1,300);
        Student s11=new Student("James",2,2600);
        Student s12=new Student("Mike",1,3000);
    
        studList.add(s1);
        studList.add(s2);
        studList.add(s3);
        studList.add(s4);
        studList.add(s5);
        studList.add(s6);
        studList.add(s7);
        studList.add(s8);
        studList.add(s9);
        studList.add(s10);
        studList.add(s11);
        studList.add(s12);
    
        return studList;
    }
    
}