Junior — Middle
Could you explain what the anyMatch method does in data streams?
sobes.tech AI
Answer from AI
The anyMatch method in Java Stream API checks whether at least one element of the stream satisfies the given condition (predicate). It returns true if at least one element matches the condition, and false if none do.
Example:
List<String> list = Arrays.asList("apple", "banana", "cherry");
boolean hasBanana = list.stream().anyMatch(s -> s.equals("banana"));
System.out.println(hasBanana); // Outputs true
This is useful for quick searches when you don't need to process all elements, just to find at least one suitable one.