bra*_*orm 3 java java-8 java-stream
这是我在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
}
}
Run Code Online (Sandbox Code Playgroud)
我希望沿着这条线走路(这显然是错误的)
list.forEach(each -> input.contains(each) ? magicMethod(input) : return );
Run Code Online (Sandbox Code Playgroud)
在您的特定情况下使用anyMatch或noneMatch更清楚:
if (list.stream().noneMatch(input::contains)) {
magicMethod(input);
}
Run Code Online (Sandbox Code Playgroud)