gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:05:19 25 4
gpt4 key购买 nike

我想使用函数 Predicate 而不是 & 运算符。这意味着我想要“第二次测试通过!!”执行我的代码后打印。

我该怎么做?

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"));
}
}

最佳答案

只需自己实现谓词:

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

& 替换为您可能需要的任何其他运算符。

关于Java8 : How to replace the bitwise inclusive and ( &) by the function Predicate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46976217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com