Junior — Middle
What value does the anyMatch method return in Java Stream API?
sobes.tech AI
Answer from AI
The anyMatch method in Java Stream API returns a boolean value. It checks whether at least one element of the stream satisfies the given predicate.
If at least one element matches the condition, anyMatch returns true; otherwise, it returns false.
Example:
List<String> list = Arrays.asList("apple", "banana", "cherry");
boolean hasBanana = list.stream().anyMatch(s -> s.equals("banana"));
System.out.println(hasBanana); // Outputs: true
In this example, anyMatch checks if the list contains the element "banana".