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?
Check if a Predicate condition is not met?
610 views Asked by ernesto fernandez At
2
There are 2 answers
0
On
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
Update: Here's a link that can help.
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.