Check if a Predicate condition is not met?

610 views Asked by At

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?

2

There are 2 answers

1
Ash_s94 On

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.

0
ΦXocę 웃 Пepeúpa ツ 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