其他事项
如何在foreach中进行break或者continue?
传统的循环写法中,我们可以使用break关键字来结束整个循环操作,使用continue关键字来跳过当前循环:
List<String> colors = new ArrayList<>(Arrays.asList("white", "black", "red", "blue", "green"));
//当遇到颜色时,结束整个循环
for (String color : colors) {
if (color.equals("red")) {
break;
}
System.out.println(color);
}
//当遇到蓝颜色时,跳过本次循环
for (String color : colors) {
if (color.equals("blue")) {
continue;
}
System.out.println(color);
}
但lambda
表达式的foreach
并不支持这两个关键字,如果想要使用break
或者continue
这种语义,可以使用以下折中的办法:
使用return
关键字实现continue
的语义
return
关键字实现continue
的语义List<String> colors = new ArrayList<>(Arrays.asList("white", "black", "red", "blue", "green"));
colors.forEach(color -> {
if (color.equals("blue")) {
return;
}
System.out.println(color);
});
//输出如下:
white
black
red
green
使用抛出异常的方式实现break
的语义
break
的语义//首先需要自定义一个异常
public class BreakException extends RuntimeException {}
//使用该异常实现break的语义
List<String> colors = new ArrayList<>(Arrays.asList("white", "black", "red", "blue", "green"));
try {
colors.forEach(color -> {
if (color.equals("blue")) {
throw new BreakException();
}
System.out.println(color);
});
} catch (BreakException e) {
System.out.println("foreach while break");
}
//输出如下
white
black
red
foreach while break
但是在实践中,并不推荐使用这种折中的方式去实现break或者continue的语义,即不应该使用foreach。而是应该根据程序的意图,去选择stream中提供的其他方法来达到目的。
比如,如果想要在一个集合中找到符合特定条件的第一个元素,可以使用filter
和findFirst
的组合:
//在colors集合中,寻找blue
List<String> colors = new ArrayList<>(Arrays.asList("white", "black", "red", "blue", "green"));
Optional<String> target = colors.stream().filter(color -> color.equals("blue")).findFirst();
String targetColor = target.get();
System.out.println(targetColor);
//输出如下
blue