
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Streams Counting Method with Examples
The counting() method is a static method of the Collectors class, which is part of the java.util.stream package. It returns a long value representing the number of elements in the stream.
Counting Number of Elements in the Stream
The following are the steps to count the number of elements in the stream:
- Import the necessary classes from java.util and java.util.stream packages.
- Create a stream of elements (either strings or integers).
- Apply the collect (Collectors.counting()) method to count the number of elements in the stream.
- Display the count of elements.
Example
The following is an example of counting the number of elements in a stream -
import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo { public static void main(String[] args) { Stream<String> stream = Stream.of("Kevin", "Jofra","Tom", "Chris", "Liam"); // count long count = stream.collect(Collectors.counting()); System.out.println("Number of elements in the stream = "+count); } }
The output of the above code will be -
Number of elements in the stream = 5
Example: Counting Elements in an Integer Stream
The following is another example of counting the number of integer elements in a stream
import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo { public static void main(String[] args) { Stream<Integer> stream = Stream.of(5, 10, 20, 40, 80, 160); // count long count = stream.collect(Collectors.counting()); System.out.println("Number of elements in the stream = "+count); } }
The output of the above code will be:
Number of elements in the stream = 6
Counting elements in a Stream of Custom Objects
The counting() method can also be applied to a stream of custom objects. In this case, we will create a class called Person with fields for name and age. We will create a list of Person objects and use the counting() method to count the number of elements in the stream.
- The list.stream() creates a sequential stream from the list.
- The collect(Collectors.counting()) method counts the number of elements in the stream.
- The getName() method retrieves the name of the person.
- The getAge() method retrieves the age of the person.
Example
Below is an example of implementing the counting() method in Java :
import java.util.*; class Person { String name; int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } } public class Demo { public static void main(String[] args) { List<Person> list = Arrays.asList(new Person("Kevin", 25), new Person("Jofra", 30), new Person("Tom", 35)); // count long count = list.stream().count(); System.out.println("Number of elements in the stream = "+count); } }
The output of the above code will be -
Number of elements in the stream = 3