Java 8 Stream Collectors groupingBy with sort Example

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

StreamGroupBy.java

package com.ehowtonow.java8;

import java.util.ArrayList;
import java.util.LinkedHashMap;
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()));

  System.out.println(result);
  
  System.out.println("Java 8 Group by a List and display the total count with sorted order.");
  Map sortedResult = new LinkedHashMap<>();

  // Sort a map and add to sortedResult
  result.entrySet().stream().sorted(Map.Entry.comparingByValue().reversed())
    .forEachOrdered(e -> sortedResult.put(e.getKey(), e.getValue()));

  System.out.println(sortedResult);
 }

}

Output
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}
Java 8 Group by a List and display the total count with sorted order.
{Virat Kohli=3, AB de Villiers=3, Faf du Plessis=2, Quinton de Kock=2, Martin Guptill=1, Babar Azam=1, Kane Williamson=1, Joe Root=1, Shikhar Dhawan=1, David Warner=1}


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java – DoubleToIntFunction Interface
  2. Java – IntToDoubleFunction Interface
  3. Java – Stream.Builder Interface
  4. Java – DoublePredicate Interface
  5. Java – BiFunction Interface
  6. Java – BiConsumer Interface
  7. Java – LongStream Interface
  8. Java 8 New Features Overview
  9. Java – DoubleStream Interface
  10. Lambda expressions are allowed only at source level 1.8 or above
  11. Java – LongPredicate Interface
  12. Java – LongToDoubleFunction Interface
  13. Java – IntUnaryOperator Interface
  14. Java – BinaryOperator Interface
  15. Java 8 Stream Filter with map Example

Be the first to comment

Leave a Reply