The filter()
method in Java, part of the java.util.stream.DoubleStream
interface, is used to select elements from a stream that match a given predicate. This method is useful when you need to create a new stream consisting of elements that satisfy a specific condition.
Table of Contents
- Introduction
filter()
Method Syntax- Understanding
filter()
- Examples
- Basic Usage
- Using
filter()
with Custom Predicate
- Real-World Use Case
- Conclusion
Introduction
The filter()
method returns a stream consisting of the elements of the original stream that match the given predicate. This method is a non-terminal intermediate operation that processes the elements of the stream based on the specified condition.
filter() Method Syntax
The syntax for the filter()
method is as follows:
DoubleStream filter(DoublePredicate predicate)
Parameters:
predicate
: ADoublePredicate
that represents the condition to be checked against the elements of the stream.
Returns:
- A
DoubleStream
consisting of the elements that match the given predicate.
Throws:
- This method does not throw any exceptions.
Understanding filter()
The filter()
method allows you to create a new stream by retaining only the elements that satisfy a specified condition. It is commonly used to perform operations like filtering out unwanted elements or selecting elements that meet certain criteria.
Examples
Basic Usage
To demonstrate the basic usage of filter()
, we will create a DoubleStream
and filter out elements that are less than 3.0.
Example
import java.util.stream.DoubleStream;
public class FilterExample {
public static void main(String[] args) {
DoubleStream doubleStream = DoubleStream.of(1.1, 2.2, 3.3, 4.4, 5.5);
// Filter elements greater than or equal to 3.0
DoubleStream filteredStream = doubleStream.filter(n -> n >= 3.0);
// Print the filtered stream
filteredStream.forEach(System.out::println);
}
}
Output:
3.3
4.4
5.5
Using filter()
with Custom Predicate
This example shows how to use filter()
with a custom predicate to select even elements from a DoubleStream
.
Example
import java.util.stream.DoubleStream;
public class CustomPredicateExample {
public static void main(String[] args) {
DoubleStream doubleStream = DoubleStream.of(1.0, 2.0, 3.0, 4.0, 5.0);
// Filter elements that are even
DoubleStream evenStream = doubleStream.filter(n -> n % 2 == 0);
// Print the filtered stream
evenStream.forEach(System.out::println);
}
}
Output:
2.0
4.0
Real-World Use Case
Filtering Sensor Data
In real-world applications, the filter()
method can be used to filter out invalid sensor readings or select readings within a specific range.
Example
import java.util.stream.DoubleStream;
public class SensorDataFilterExample {
public static void main(String[] args) {
DoubleStream sensorData = DoubleStream.of(25.3, 26.7, 24.8, 30.1, 27.5);
// Filter sensor readings within the range 25.0 to 28.0
DoubleStream filteredData = sensorData.filter(temp -> temp >= 25.0 && temp <= 28.0);
// Print the filtered sensor readings
filteredData.forEach(System.out::println);
}
}
Output:
25.3
26.7
27.5
Conclusion
The DoubleStream.filter()
method is used to create a new stream consisting of elements that match a given predicate. This method is particularly useful for filtering elements based on specific conditions. By understanding and using this method, you can efficiently manage and manipulate streams of double values in your Java applications.
Comments
Post a Comment
Leave Comment