The allMatch()
method in Java, part of the java.util.stream.DoubleStream
interface, is used to check if all elements of a stream match a given predicate. This method is useful when you need to verify that all elements in a stream satisfy a specific condition.
Table of Contents
- Introduction
allMatch()
Method Syntax- Understanding
allMatch()
- Examples
- Basic Usage
- Using
allMatch()
with Custom Predicate
- Real-World Use Case
- Conclusion
Introduction
The allMatch()
method returns true
if all elements of the stream match the provided predicate. If any element does not match the predicate, the method returns false
. This is a terminal operation and it short-circuits as soon as the answer is determined.
allMatch() Method Syntax
The syntax for the allMatch()
method is as follows:
boolean allMatch(DoublePredicate predicate)
Parameters:
predicate
: ADoublePredicate
that represents the condition to be checked against the elements of the stream.
Returns:
true
if all elements match the predicate; otherwise,false
.
Throws:
- This method does not throw any exceptions.
Understanding allMatch()
The allMatch()
method allows you to check if every element in a DoubleStream
satisfies a given condition. If the stream is empty, it returns true
, as there are no elements to violate the predicate.
Examples
Basic Usage
To demonstrate the basic usage of allMatch()
, we will create a DoubleStream
and check if all elements are greater than 0.
Example
import java.util.stream.DoubleStream;
public class AllMatchExample {
public static void main(String[] args) {
DoubleStream numbers = DoubleStream.of(1.1, 2.2, 3.3, 4.4, 5.5);
// Check if all elements are greater than 0
boolean allPositive = numbers.allMatch(n -> n > 0);
System.out.println("All elements are positive: " + allPositive);
}
}
Output:
All elements are positive: true
Using allMatch()
with Custom Predicate
This example shows how to use allMatch()
with a custom predicate to check if all elements in a DoubleStream
are even.
Example
import java.util.stream.DoubleStream;
public class CustomPredicateExample {
public static void main(String[] args) {
DoubleStream numbers = DoubleStream.of(2.0, 4.0, 6.0, 8.0, 10.0);
// Check if all elements are even
boolean allEven = numbers.allMatch(n -> n % 2 == 0);
System.out.println("All elements are even: " + allEven);
}
}
Output:
All elements are even: true
Real-World Use Case
Verifying Temperatures within a Safe Range
In real-world applications, the allMatch()
method can be used to verify if all temperature readings are within a safe range.
Example
import java.util.stream.DoubleStream;
public class TemperatureCheckExample {
public static void main(String[] args) {
DoubleStream temperatures = DoubleStream.of(36.5, 37.0, 36.8, 37.1, 36.9);
// Check if all temperatures are within the safe range (36.5 to 37.5 degrees Celsius)
boolean allSafe = temperatures.allMatch(temp -> temp >= 36.5 && temp <= 37.5);
System.out.println("All temperatures are within the safe range: " + allSafe);
}
}
Output:
All temperatures are within the safe range: true
Conclusion
The DoubleStream.allMatch()
method is used to check if all elements of a stream match a given predicate. This method is particularly useful for verifying that all elements in a stream satisfy a specific condition. By understanding and using this method, you can efficiently perform validation checks on streams of double values in your Java applications.
Comments
Post a Comment
Leave Comment