The count()
method in Java, part of the java.util.stream.Stream
interface, is used to count the number of elements in the stream. This method is useful when you need to determine the size of the stream.
Table of Contents
- Introduction
count()
Method Syntax- Understanding
count()
- Examples
- Basic Usage
- Using
count()
with Filtered Streams
- Real-World Use Case
- Conclusion
Introduction
The count()
method is a terminal operation that returns the count of elements in the stream. It is a simple and effective way to determine the number of elements in a stream.
count() Method Syntax
The syntax for the count()
method is as follows:
long count()
Parameters:
- This method does not take any parameters.
Returns:
- The count of elements in the stream as a
long
.
Throws:
- This method does not throw any exceptions.
Understanding count()
The count()
method processes the elements of the stream and returns the total number of elements. This is particularly useful for obtaining the size of the stream, especially after applying various intermediate operations such as filtering or mapping.
Examples
Basic Usage
To demonstrate the basic usage of count()
, we will create a Stream
and use count()
to count the number of its elements.
Example
import java.util.stream.Stream;
public class CountExample {
public static void main(String[] args) {
Stream<String> stream = Stream.of("apple", "banana", "cherry");
// Use count() to count the number of elements in the stream
long count = stream.count();
// Print the count
System.out.println("Count: " + count);
}
}
Output:
Count: 3
Using count()
with Filtered Streams
This example shows how to use count()
in combination with other stream operations, such as filtering.
Example
import java.util.stream.Stream;
public class CountWithFilterExample {
public static void main(String[] args) {
Stream<String> stream = Stream.of("apple", "banana", "cherry", "apricot", "blueberry");
// Filter elements that start with 'a' and count them
long count = stream.filter(s -> s.startsWith("a")).count();
// Print the count of filtered elements
System.out.println("Count of elements starting with 'a': " + count);
}
}
Output:
Count of elements starting with 'a': 2
Real-World Use Case
Counting Employees Above a Certain Age
In real-world applications, the count()
method can be used to count the number of employees above a certain age from a stream of employee objects.
Example
import java.util.stream.Stream;
public class EmployeeCountExample {
static class Employee {
String name;
int age;
Employee(String name, int age) {
this.name = name;
this.age = age;
}
int getAge() {
return age;
}
}
public static void main(String[] args) {
Stream<Employee> employeeStream = Stream.of(
new Employee("Alice", 30),
new Employee("Bob", 25),
new Employee("Charlie", 35),
new Employee("David", 28)
);
// Count employees above the age of 30
long count = employeeStream.filter(employee -> employee.getAge() > 30).count();
// Print the count of employees above the age of 30
System.out.println("Count of employees above 30: " + count);
}
}
Output:
Count of employees above 30: 1
Conclusion
The Stream.count()
method is used to count the number of elements in a stream. This method is particularly useful for determining the size of the stream after applying various intermediate operations. By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, obtaining counts as needed.
Comments
Post a Comment
Leave Comment