Java 8 Counting with Collectors | Collectors.counting method tutorial with examples
Java 8 Counting with Collectors tutorial explains, with examples, how to use the predefined
Where,
- output is a
The working of the counting collector is pretty straightforward. It is a terminal operationClick to Read Tutorial explaining intermediate & terminal Stream operations which returns the total count of elements in the stream which reach the
It's also worthwhile to know that internally the counting collector actually delegates the counting job to the 'reducing collector' with the call
Java 8 code example showing
OUTPUT of the above code
Explanation of the code
Collector
returned by java.util.Stream.Collectors
class' counting()
method to count the number of elements in a Stream
. The tutorial starts off with the formal definition of the Collectors.counting()
method, then explains its working, and finally shows the method's usage via a Java code example.
(Note - This tutorial assumes that you are familiar with basics of Java 8 CollectorsRead Tutorial explaining basics of Java 8 Collectors.)
Predefined Collector
returned by Collectors.counting()
method
Collectors.counting()
method is defined with the following signature - public static <T> Collector<T, ?, Long> counting()
- output is a
Collector
, acting on a Stream of elements of type T
, with its finisherClick to Read tutorial on 4 components of Collectors incl. 'finisher' returning the 'collected' value of type Long
The working of the counting collector is pretty straightforward. It is a terminal operationClick to Read Tutorial explaining intermediate & terminal Stream operations which returns the total count of elements in the stream which reach the
collect()
method after undergoing various pipelinedClick to Read Tutorial explaining concept of Pipelines in Computing stream operations such as filtering, reduction etc.It's also worthwhile to know that internally the counting collector actually delegates the counting job to the 'reducing collector' with the call
reducing(0L, e -> 1L, Long::sum)
. But this call is internal to the implementation and need not be taken into consideration before terminating your stream with the counting Collector.Collector.counting()
method's usage
Java 8 code example showing Collectors.counting() usage
package com.javabrahman.java8.collector;
import com.javabrahman.java8.Employee;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class CountingWithCollectors {
static List<Employee> employeeList = Arrays.asList(new Employee("Tom Jones", 45),
new Employee("Harry Major", 25),
new Employee("Ethan Hardy", 65),
new Employee("Nancy Smith", 22),
new Employee("Deborah Sprightly", 29));
public static void main(String args[]){
Long count=employeeList.stream().collect(Collectors.counting());
System.out.println("Employee count: "+count);
}
}
//Employee.java(POJO Class)
package com.javabrahman.java8;
public class Employee {
private String name;
private Integer age;
public Employee(String name, Integer age) {
this.name = name;
this.age = age;
}
//Getters and Setters of name & age go here
public String toString(){
return "Employee Name:"+this.name
+" Age:"+this.age;
}
//Standard equals() and hashcode() implementations go here
}
Employee count: 5
Employee.java
is the POJO class i.e. the type of which the Stream is created.CountingWithCollectors
class contains a static list of 5Employee
objects namedemployeeList
.- In the
main()
method ofCountingWithCollectors
class a stream ofEmployee
objects is created by invokingemployeeList.stream()
. This stream is then terminated by invoking thecollect()
method with theCollector
returned by theCollectors.counting()
method passed as a parameter. - As expected, the Stream terminates and the counting
Collector
returns the count of the stream elements(employees) as aLong
value. - Lastly, the employee count is printed and is
5
as expected.
Collector
returned by the Collectors.counting()
method. We looked at the formal definition of the Collectors.counting()
method, had a brief look at what actually happens inside the method, and finally saw the Collectors.counting()
method in action with a Java 8 code example and its explanation.Java 8 Collectors' Tutorials on JavaBrahman
Understanding Basics of Java 8 CollectorsClick to Read Tutorial explaining basics of Java 8 CollectorsCollectors.groupingBy()Click to Read Tutorial on Grouping with CollectorsCollectors.partitioningBy()Click to Read Partitioning using Collectors TutorialCollectors.counting()Click to Read Counting with Collectors TutorialCollectors.maxBy()/minBy()Click to Read Tutorial on finding max/min with CollectorsCollectors.joining()Click to Read Tutorial on joining as a String using CollectorsCollectors.collectingAndThen()Click to Read Tutorial on collectingAndThen CollectorCollectors.averagingInt() /averagingLong() /averagingDouble()Click to Read Tutorial on Averaging CollectorCollectors.toCollection()Click to Read Tutorial on Collectors.toCollection CollectorCollectors.mapping()Click to Read Tutorial on Mapping Collector
Understanding Basics of Java 8 CollectorsClick to Read Tutorial explaining basics of Java 8 CollectorsCollectors.groupingBy()Click to Read Tutorial on Grouping with CollectorsCollectors.partitioningBy()Click to Read Partitioning using Collectors TutorialCollectors.counting()Click to Read Counting with Collectors TutorialCollectors.maxBy()/minBy()Click to Read Tutorial on finding max/min with CollectorsCollectors.joining()Click to Read Tutorial on joining as a String using CollectorsCollectors.collectingAndThen()Click to Read Tutorial on collectingAndThen CollectorCollectors.averagingInt() /averagingLong() /averagingDouble()Click to Read Tutorial on Averaging CollectorCollectors.toCollection()Click to Read Tutorial on Collectors.toCollection CollectorCollectors.mapping()Click to Read Tutorial on Mapping Collector