How to use Collectors.groupingBy() in Java 8

 295 total views,  1 views today

Welcome to Java 8 series. In this tutorial, you will learn about how to use Collectors.groupingBy() in Java 8.

If you would like to learn about Java 8 Stream operations.Please check out this link.I request you to have a basic understanding of Java 8 features and stream operations as a prerequisite.Please have a look at Java 8 complete guide.

So let us get started with our learning.

Collectors.groupingBy()

Generally, ‘GROUP BY’ operations are useful to group your input data based on a certain parameter.You input data will be grouped based on a certain key.The same functionality is implemented in Java 8 through Collectors.groupingBy() operation.If you are familiar with SQL’s GROUP By, then you can easily understand how the same works in Java 8 streams.

There are three variants of Collectors.groupingBy() operation in Java 8 and let us see an example for each variant.

public static <T, K> Collector<T, ?, Map<K, List<T>>> groupingBy(
   Function<? super T, ? extends K> classifier)


public static <T, K, A, D> Collector<T, ?, Map<K, D>> groupingBy(
   Function<? super T, ? extends K> classifier,
   Collector<? super T, A, D> downstream)


public static <T, K, D, A, M extends Map<K, D>> Collector<T, ?, M> groupingBy(
   Function<? super T, ? extends K> classifier,
   Supplier<M> mapFactory,
   Collector<? super T, A, D> downstream)

Collectors.groupingBy() – With function and classifier

In this example, we are grouping the list of fruits based on the name and apply count operation.

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

public class StaticReference {
 public static void main(String[] args) {

  List<String> fruits = Arrays.asList("apple", "mango", "pomo",
    "banana", "orange", "apple", "pomo");

  Map<String, Long> output = fruits.stream().collect(Collectors
    .groupingBy(item -> item, Collectors.counting()));

  output.forEach((k, v) -> System.out.println(k + " -  " + v));
 }

}

Output

orange -  1
banana -  1
apple -  2
pomo -  2
mango -  1

Note – Instead of using item->item, we can also use Function.identity(). Both are same.

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

public class StaticReference {
 public static void main(String[] args) {

  List<String> fruits = Arrays.asList("apple", "mango", "pomo",
    "banana", "orange", "apple", "pomo");

  Map<String, Long> output = fruits.stream().collect(Collectors
    .groupingBy(Function.identity(), Collectors.counting()));

  output.forEach((k, v) -> System.out.println(k + " -  " + v));
 }

}

Collectors.groupingBy() using Java Object

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

class Person {

 String name;
 String age;
 String city;

 public Person(String name, String age, String city) {
  super();
  this.name = name;
  this.age = age;
  this.city = city;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getAge() {
  return age;
 }

 public void setAge(String age) {
  this.age = age;
 }

 public String getCity() {
  return city;
 }

 public void setCity(String city) {
  this.city = city;
 }

}

public class StaticReference {
 public static void main(String[] args) {

  List<Person> list = getPersons();

  //Group based on City
  Map<String, List<Person>> output = list.stream().collect(Collectors
    .groupingBy(Person::getCity));

  output.forEach((k, v) -> System.out.println(k + " -  " + v.size()));
 }

 public static List<Person> getPersons() {
  List<Person> persons = new ArrayList<Person>();

  Person person1 = new Person("John", "24", "US");
  Person person2 = new Person("Peter", "23", "UK");
  Person person3 = new Person("Steve", "35", "US");
  Person person4 = new Person("Smith", "29", "UK");
  Person person5 = new Person("Park", "46", "US");
  Person person6 = new Person("Bose", "57", "AUS");
  Person person7 = new Person("Mark", "34", "US");
  Person person8 = new Person("waugh", "45", "AUS");
  Person person9 = new Person("Chris", "23", "US");

  persons.add(person1);
  persons.add(person2);
  persons.add(person3);
  persons.add(person4);
  persons.add(person5);
  persons.add(person6);
  persons.add(person7);
  persons.add(person8);
  persons.add(person9);

  return persons;
 }

}

Output

UK -  2
US -  5
AUS -  2

Collectors.groupingBy() – With classifier and collector

public class StaticReference {
 public static void main(String[] args) {

  List<Person> list = getPersons();


  //Group based on age and count
  Map<String, Long> output = list.stream().collect(Collectors
    .groupingBy(Person::getCity,Collectors.counting()));

  output.forEach((k, v) -> System.out.println(k + " -  " + v));
 }

}
public class StaticReference {
 public static void main(String[] args) {

  List<Person> list = getPersons();

  // Find out average of age based on City
  Map<String, Double> output = list.stream()
    .collect(Collectors.groupingBy(Person::getCity,
      Collectors.averagingInt(Person::getAge)));

  output.forEach((k, v) -> System.out.println(k + " -  " + v));
 }

}

Output

UK -  26.0
US -  32.4
AUS -  51.0

 

Collectors.groupingBy() – multilevel grouping

public class StaticReference {
 public static void main(String[] args) {

  List<Person> list = getPersons();

  // Find out average of age based on City
  Map<String, Map<Integer, List<Person>>> output = list.stream()
    .collect(Collectors.groupingBy(Person::getCity,
      Collectors.groupingBy(Person::getAge)));

  output.forEach((k, v) -> System.out.println(k + " -  " + v));
 }
}

References : Collectors.groupingBy() in Java 8

I hope you like this tutorial. Thanks for reading and Please leave us a comment below!

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.