In this tutorial you’ll learn about the count()
method in the Java Stream API with the help of few examples.
count() method in Java
count() method returns the count of elements in the stream.
Syntax of count() method-
long count()
Some important points about count() method-
- It is a special case of a reduction operation which means it takes a sequence of input elements and reduce them into a single summary result.
- It is a terminal operation which means after count() operation the stream pipeline is considered consumed, and can no longer be used.
count() method Java examples
1. In the first example count() is used to get the count of elements in a List where List is used as a stream source.
public class StreamCountDemo { public static void main(String[] args) { List<Integer> numList = Arrays.asList(7, 5, 18, -11, 22, -8); long elementCount = numList.stream().count(); System.out.println("Count of elements- " + elementCount); } }
Output
Count of elements- 6
2. Since count is a terminal operation so stream is considered closed after count operation but before count, intermediate operations like filter can be used to filter out certain elements and then get the count of the resulting stream. For example if we want count of positive elements in a list.
public class StreamCountDemo { public static void main(String[] args) { List<Integer> numList = Arrays.asList(7, 5, 18, -11, 22, -8); long elementCount = numList.stream().filter(n -> n > 0).count(); System.out.println("Count of elements- " + elementCount); } }
Output
Count of elements- 4
3. In the following example count is used to get the count of employees having salary greater than 10000.
Employee class
public class Employee { private String empId; private int age; private String name; private char gender; private int salary; Employee(String empId, int age, String name, char gender, int salary){ this.empId = empId; this.age = age; this.name = name; this.gender = gender; this.salary = salary; } public String getEmpId() { return empId; } public int getAge() { return age; } public String getName() { return name; } public char getGender() { return gender; } public int getSalary() { return salary; } @Override public String toString() { return "Emp Id: " + getEmpId() + " Name: " + getName() + " Age: " + getAge(); } }
public class StreamCountDemo { public static void main(String[] args) { List<Employee> empList = Arrays.asList(new Employee("E001", 40, "Ram", 'M', 5000), new Employee("E002", 35, "Shelly", 'F', 7000), new Employee("E003", 40, "Remington", 'M', 5000), new Employee("E004", 37, "Bianca", 'F', 11000), new Employee("E005", 35, "Dominic", 'M', 7000), new Employee("E006", 28, "Amanda", 'F', 14000)); long elementCount = empList.stream().filter(e -> e.getSalary() > 10000).count(); System.out.println("Count of elements- " + elementCount); } }
Output
Count of elements- 2
That's all for this topic Java Stream - count() With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Advanced Tutorial Page
Related Topics
You may also like-
No comments:
Post a Comment