Aguid
Aguid

Reputation: 1043

Java8: How to replace the bitwise inclusive and ( &) by the function Predicate?

I want to use the function Predicate instead of the & operator. it means that I want that "Second Test is passed !!" be printed after the execution of my code.

How can I do it ?

public class BitwiseInclusiveAND{

    public static void main(String[] args) {
        final Predicate<String> condition1 = (Predicate<String>) (arg -> arg != null);
        final Predicate<String> condition2 = (Predicate<String>) (arg -> {
            System.out.println("Second Test is passed !!");
            return arg.equals("Hello");
        });
        Predicate<String> equalsStrings
                = condition1.and(condition2); // Here I want to execute the condition2 even if condition 1 = true 
        System.out.println(equalsStrings.test("Hello"));
    }
}

Upvotes: 2

Views: 181

Answers (1)

Andy Turner
Andy Turner

Reputation: 140494

Just implement the predicate yourself:

Predicate<String> or = s -> condition1.test(s) & condition2.test(s);

Replacing the & with whatever other operator you might want.

Upvotes: 5

Related Questions