728x90 AdSpace

Sunday, 25 June 2017

Java 8 Stream Collectors groupingBy Example

In this article we are going to see about Java 8 Stream Collectors  groupingBy with example program.


package com.javatutorialcorner.java8;

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

public class StreamGroupBy {

 public static void main(String[] args) {
  List batsmanRanking = new ArrayList<>();
  batsmanRanking.add("Virat Kohli");
  batsmanRanking.add("AB de Villiers");
  batsmanRanking.add("David Warner");
  batsmanRanking.add("Joe Root");
  batsmanRanking.add("Kane Williamson");
  batsmanRanking.add("Quinton de Kock");
  batsmanRanking.add("AB de Villiers");
  batsmanRanking.add("Faf du Plessis");
  batsmanRanking.add("Virat Kohli");
  batsmanRanking.add("Babar Azam");
  batsmanRanking.add("Quinton de Kock");
  batsmanRanking.add("Martin Guptill");
  batsmanRanking.add("Shikhar Dhawan");
  batsmanRanking.add("AB de Villiers");
  batsmanRanking.add("Virat Kohli");
  batsmanRanking.add("Faf du Plessis");

  System.out.println("Java 8 Group by a List and display the total count.");

  Map result = batsmanRanking.stream()
    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));



Java 8 Group by a List and display the total count.
{Faf du Plessis=2, Virat Kohli=3, Martin Guptill=1, Babar Azam=1, Quinton de Kock=2, Kane Williamson=1, AB de Villiers=3, Joe Root=1, Shikhar Dhawan=1, David Warner=1}

Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments


Post a Comment

Item Reviewed: Java 8 Stream Collectors groupingBy Example Rating: 5 Reviewed By: Annamalai Thangaraj