In Java 8 and later versions, the Collectors
class in the java.util.stream
package provides various utility methods to perform reduction operations on streams. One of the powerful features of the Collectors
class is the ability to group and count elements in a list or stream.
To group and count elements in a list, you can use the Collectors.groupingBy()
and Collectors.counting()
methods in combination. The groupingBy()
method is used to create a Map
where the elements are grouped based on a specific classifier, and the counting()
method is used to count the occurrences of each group.
How to use the Collectors class in Java 8 to group and count elements in a list
How to use the Collectors class in Java 8 to group and count elements in a list? Share on X
Here’s an example of the usage of Collectors
class to group and count elements in a list:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | /** * Use of Collectors class to count and group elements in a List in Java 8 * @author Deepak Verma * */ import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Collectors_Class_To_Count_and_Group_Elements_In_a_List_In_Java8 { public static void main(String[] args) { List<String> programmingWebsites = Arrays.asList("Techndeck", "MkYong", "Baeldung", "JavaTPoint", "Java67", "MkYong", "Techndeck", "Java67", "Java67", "Java67", "Techndeck", "Baeldung", "Baeldung"); // Group and count elements in the list Map<String, Long> websitesCount_Map = programmingWebsites.stream() .collect(Collectors.groupingBy(website -> website, Collectors.counting())); // Print out the result for (Map.Entry<String, Long> entry : websitesCount_Map.entrySet()) { System.out.println(entry.getKey() + " = " + entry.getValue()); } } } |
Output:
1 2 3 4 5 | Baeldung = 3 Java67 = 4 Techndeck = 3 JavaTPoint = 1 MkYong = 2 |
In the example above, we have a list of websites and our objective is to count the occurrences of each website present in the list. First, we will use the stream()
method to convert the list to a stream, and then we use the collect()
method along with Collectors.groupingBy()
to group the elements by the website name. The second parameter to groupingBy()
is Collectors.counting()
, which will count the occurrences of each group. The websiteCount_map
contains the result, where each website name is associated with its count in the original list. The result shows the count of each website in the list. So, what we have learnt is that by using the Collectors.groupingBy()
and Collectors.counting()
methods, we can easily perform grouping and counting operations on lists in Java 8 or any other later versions.
Learn to use Collectors() class to group and count elements in a List in Java 8..!!! Share on X
Do you like this Post? – then check my other helpful posts:
- Convert a Stream to a List in Java 8
- Stream maptoint in Java 8 with examples
- Double the numbers of specified ArrayList using Streams
- Double the even / odd numbers of a specified ArrayList using Streams
- How to check if Number is Prime or not using Streams
- Retrieve Even/Odd Numbers within the Range using Java 8 Streams
- How to add/sum up the ArrayList integers using Java8 Streams
- Generate Prime Numbers in Java 8 using Streams
- Comparator example – Collections sort with/without Lambda in Java 8
- How to pass function as a parameter in a method in Java 8?
- Remove duplicates from ArrayList in Java 8
- ForEach examples for Map/List in Java 8
Other Useful References: