I am printing a list after testing my predicate and I would like to know if there is a way to check if the condition was not met?
Asked
Active
Viewed 539 times
-2
-
Check `if (!predicate.test(input)) { /* do something */ }` – Andy Turner Nov 09 '17 at 19:01
-
How about `predicate.negate().test(input)` ? – KitKarson Nov 09 '17 at 19:07
-
Appreciate the response however this solution did not work. – ernesto fernandez Nov 09 '17 at 19:35
2 Answers
3
Update: Here's a link that can help.
public static Predicate<Employee> isAdultMale() {
return p -> p.getAge() > 21 && p.getGender().equalsIgnoreCase("M");
}
In the above example, this is a function that returns the predicate if the condition is met. So similarly maybe you can use this, and when nothing is printed, that's how you know your condition wasn't met.

Ash_s94
- 787
- 6
- 19
1
since you are working with a list in order to check the predicate, then option 1
Predicate<Integer> p = x -> x > 1;
List<Integer> mL = new ArrayList<>();
mL.add(1);
mL.add(2);
mL.add(3);
and use noneMatch
or anyMatch
System.out.println(mL.stream().anyMatch(p));
System.out.println(mL.stream().noneMatch(p));
read the doc so you can get the right interpretation of that result

ΦXocę 웃 Пepeúpa ツ
- 47,427
- 17
- 69
- 97