The allMatch() method in Java Streams is a terminal operation that returns true if all elements of the stream match the given predicate, or if the stream is empty. It is short-circuiting, meaning it may stop evaluating elements as soon as the result is determined.
Example:
import java.util.stream.Stream;
class GFG {
public static void main(String[] args) {
Stream<Integer> numbers = Stream.of(2, 4, 6, 8);
boolean result = numbers.allMatch(n -> n % 2 == 0);
System.out.println(result);
}
}
Output
true
Explanation:
- A stream of integers 2, 4, 6, 8 is created using Stream.of().
- The allMatch(n -> n % 2 == 0) method checks whether all numbers are even.
- Since all elements satisfy the condition, it returns true.
- The result is printed to the console.
Syntax
boolean allMatch(Predicate<? super T> predicate)
- Parameters: "predicate" a functional interface representing a condition to test each element.
- Return Value: Returns true if all elements match the predicate or the stream is empty.
Example 1: This program uses allMatch() to check if all elements in a stream of numbers are divisible by 3.
import java.util.stream.Stream;
class GFG {
public static void main(String[] args) {
Stream<Integer> numbers = Stream.of(3, 6, 9, 10);
boolean result = numbers.allMatch(n -> n % 3 == 0);
System.out.println(result);
}
}
Output
false
Explanation:
- A stream of integers 3, 6, 9, 10 is created using Stream.of().
- The allMatch(n -> n % 3 == 0) method checks whether each number is divisible by 3.
- Since 10 is not divisible by 3, the method returns false.
Example 2: This program uses allMatch() to check if all strings in a stream have length greater than 2.
import java.util.stream.Stream;
class GFG {
public static void main(String[] args) {
Stream<String> strings = Stream.of("Java", "GFG", "Stream");
boolean result = strings.allMatch(s -> s.length() > 2);
System.out.println(result);
}
}
Output
true
Explanation:
- A stream of strings "Java", "GFG", "Stream" is created using Stream.of().
- The allMatch(s -> s.length() > 2) method checks whether each string has more than 2 characters.
- Since all strings satisfy the condition, it returns true.
Example 3: This program uses allMatch() to check if all strings in a stream start with an uppercase character.
import java.util.stream.Stream;
class GFG {
public static void main(String[] args) {
Stream<String> strings = Stream.of("Java", "gFG", "Stream");
boolean result = strings.allMatch(s -> Character.isUpperCase(s.charAt(0)));
System.out.println(result);
}
}
Output
false
Explanation:
- A stream of strings "Java", "gFG", "Stream" is created using Stream.of().
- The allMatch(s -> Character.isUpperCase(s.charAt(0))) method checks whether the first character of each string is uppercase.
- Since "gFG" starts with a lowercase letter, the method returns false.
Example 4: This program demonstrates that a stream cannot be reused after a terminal operation like allMatch().
import java.util.stream.Stream;
class GFG {
public static void main(String[] args) {
Stream<String> strings = Stream.of("Java", "GFG", "Stream");
boolean res1 = strings.allMatch(s -> s.length() > 2);
// Uncommenting the next line will throw IllegalStateException because the stream
// is already consumed
// boolean res2 = strings.allMatch(s -> Character.isUpperCase(s.charAt(0)));
System.out.println(res1);
// System.out.println(res2);
}
}
Output
true
Explanation:
- A stream of strings "Java", "GFG", "Stream" is created using Stream.of().
- The first terminal operation allMatch(s -> s.length() > 2) checks whether all strings have length greater than 2 and returns true.
- Attempting to use the same stream again (commented res2) would throw IllegalStateException because streams can be consumed only once.
- The result of the first operation is printed.