anyMatch() — returns true if at least one element matches the predicate. noneMatch() — returns true if no element matches the predicate. They are logical opposites — anyMatch(p) is equivalent to !noneMatch(p).
anyMatch():
1boolean hasEven = numbers.stream()2 .anyMatch(n -> n % 2 == 0);
noneMatch():
!anyMatch(predicate.negate()).1boolean allPositive = numbers.stream()2 .noneMatch(n -> n <= 0);
Relationship:
noneMatch(p) == !anyMatch(p.negate())anyMatch(p) == !noneMatch(p.negate())Empty stream behavior:
Performance considerations:
1// WRONG: verbose way to check noneMatch2boolean noNegatives = !numbers.stream().anyMatch(n -> n < 0);34// CORRECT: use noneMatch() for clarity5boolean noNegatives = numbers.stream().noneMatch(n -> n < 0);
Real-world usage:
1// Check if any order is overdue2boolean hasOverdue = orders.stream()3 .anyMatch(o -> o.getDueDate().isBefore(LocalDate.now()));45// Check if no user is banned6boolean noBanned = users.stream()7 .noneMatch(User::isBanned);89// Check if all products are in stock10boolean allInStock = products.stream()11 .allMatch(Product::isInStock);