这是我在 Java 7 中所做的:
public class Sample {
private List<String> list = Lists.newArrayList("helloworld", "foobar", "newyork");
public void performOperation(String input) {
boolean found = false;
for (String each : list) {
if (input.contains(each)) {
found = true;
}
}
if (!found) {
magicMethod(input);
}
}
public void magicMethod(String input) {
// do the real magic here
}
}
我希望沿着这条线走下去(由于显而易见的原因这是错误的)
list.forEach(each -> input.contains(each) ? magicMethod(input) : return );
最佳答案
使用 anyMatch
或 noneMatch
,以在您的特定情况下更清楚的为准:
if (list.stream().noneMatch(input::contains)) {
magicMethod(input);
}
关于Java 8 语法迭代并调用基于所有元素的否定谓词的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32705510/