Saturday 12, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 25 June 2017

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

01.package com.javatutorialcorner.java8;
02. 
03.import java.util.ArrayList;
04.import java.util.LinkedHashMap;
05.import java.util.List;
06.import java.util.Map;
07.import java.util.function.Function;
08.import java.util.stream.Collectors;
09. 
10.public class StreamGroupBy {
11. 
12. public static void main(String[] args) {
13.  List<string> batsmanRanking = new ArrayList<>();
14.  batsmanRanking.add("Virat Kohli");
15.  batsmanRanking.add("AB de Villiers");
16.  batsmanRanking.add("David Warner");
17.  batsmanRanking.add("Joe Root");
18.  batsmanRanking.add("Kane Williamson");
19.  batsmanRanking.add("Quinton de Kock");
20.  batsmanRanking.add("AB de Villiers");
21.  batsmanRanking.add("Faf du Plessis");
22.  batsmanRanking.add("Virat Kohli");
23.  batsmanRanking.add("Babar Azam");
24.  batsmanRanking.add("Quinton de Kock");
25.  batsmanRanking.add("Martin Guptill");
26.  batsmanRanking.add("Shikhar Dhawan");
27.  batsmanRanking.add("AB de Villiers");
28.  batsmanRanking.add("Virat Kohli");
29.  batsmanRanking.add("Faf du Plessis");
30. 
31.  System.out.println("Java 8 Group by a List and display the total count.");
32. 
33.  Map<string long=""> result = batsmanRanking.stream()
34.    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
35. 
36.  System.out.println(result);
37.   
38.  System.out.println("Java 8 Group by a List and display the total count with sorted order.");
39.  Map<string long=""> sortedResult = new LinkedHashMap<>();
40. 
41.  // Sort a map and add to sortedResult
42.  result.entrySet().stream().sorted(Map.Entry.<string long="">comparingByValue().reversed())
43.    .forEachOrdered(e -> sortedResult.put(e.getKey(), e.getValue()));
44. 
45.  System.out.println(sortedResult);
46. }
47. 
48.}
49.</string></string></string></string>

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}




Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • How to replace the element from List Following example shows how to replace all the occurrence of element with another element using Collections.replaceAll(list, oldVal, newVal) method in Java. <String> boolean java.util.Collections.replaceAll(List<St… Read More
  • How to shuffle the element of collection (List) Following example shows how to shuffle the element of Collection (List) using Collections.shuffle(list); method in Java. void java.util.Collections.shuffle(List<?> list)shufflepublic static void shuffle(List<?> … Read More
  • How to get the size of Collection Following example shows how to get the size of collection using collection.size() method in Java. int java.util.List.size()sizeint size() Returns the number of elements in this list. If this list contains more than Integer.… Read More
  • Java StAX API OverviewThe StAX API exposes methods for iterative, event-based processing of XML documents. The StAX API is bidirectional, enabling both reading and writing of XML documents.The StAX API is really two distinct API sets 1. Cursor AP… Read More
  • String Concatenation in JavaFollowing example shows String concatenation in Java using + operator and StringBuffer.append() method. Sample Programpackage com.javatutorialcorner.javastring;public class StringConcatenation { public static void main(Str… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Java 8 Stream Collectors groupingBy with sort Example Rating: 5 Reviewed By: eHowToNow