The allMatch()
method in Java is part of the java.util.stream.Stream
interface. In this guide, we will learn how to use allMatch()
method in Java with practical examples and real-world use cases to better understand its functionality.
Table of Contents
- Introduction
allMatch()
Method Syntax- Understanding
allMatch()
- Examples
- Basic Usage
- Using
allMatch()
with Complex Predicates
- Real-World Use Case
- Conclusion
Introduction
The allMatch()
method is a terminal operation that returns true
if all elements of the stream match the provided predicate, otherwise it returns false
.
This method is particularly useful for scenarios where you need to ensure that all elements in a stream meet a certain condition.
allMatch() Method Syntax
The syntax for the allMatch()
method is as follows:
boolean allMatch(Predicate<? super T> predicate)
Parameters:
predicate
: APredicate
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 processes each element of the stream and returns true
if all elements match the given predicate. If any element does not match the predicate, it short-circuits and returns false
.
Examples
Basic Usage
To demonstrate the basic usage of allMatch()
, we will create a Stream
and use allMatch()
to check if all elements are positive.
Example
import java.util.stream.Stream;
public class AllMatchExample {
public static void main(String[] args) {
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
// Use allMatch() to check if all elements are positive
boolean allPositive = stream.allMatch(n -> n > 0);
System.out.println("All elements are positive: " + allPositive);
}
}
Output:
All elements are positive: true
Using allMatch()
with Complex Predicates
This example shows how to use allMatch()
with a more complex predicate to check if all elements are even.
Example
import java.util.stream.Stream;
public class AllMatchComplexExample {
public static void main(String[] args) {
Stream<Integer> stream = Stream.of(2, 4, 6, 8, 10);
// Use allMatch() to check if all elements are even
boolean allEven = stream.allMatch(n -> n % 2 == 0);
System.out.println("All elements are even: " + allEven);
}
}
Output:
All elements are even: true
Real-World Use Case
Validating User Input
In real-world applications, the allMatch()
method can be used to validate user input, such as ensuring all inputs are within a certain range.
Example
import java.util.stream.Stream;
public class ValidateUserInputExample {
public static void main(String[] args) {
Stream<Integer> userInputs = Stream.of(25, 30, 35, 40, 45);
// Use allMatch() to check if all inputs are within the range 20 to 50
boolean validInputs = userInputs.allMatch(input -> input >= 20 && input <= 50);
System.out.println("All user inputs are valid: " + validInputs);
}
}
Output:
All user inputs are valid: true
Conclusion
The Stream.allMatch()
method is used to check if all elements of the stream match a given predicate. This method is particularly useful for ensuring that all elements in a stream satisfy a specific condition.
By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, ensuring data integrity and meeting specific criteria.