对于java8中的特殊写法lamada表达式中,不能使用break,会提示错误;
java8中使用return,会跳出当前循环,继续下一次循环,作用类似continue;
java8中使用foreach,但是不是lamada表达式写法,可以正常使用break或者return,可以直接跳出循环.
@Slf4j
public class CustomForEachUsage {
public static void main(String[] args) {
Stream ints = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List result = new ArrayList<>();
CustomForEach.forEach(ints, (elem, breaker) -> {
if (elem >= 5 ) {
breaker.stop();
} else {
result.add(elem);
}
});
log.info(result.toString());
}
}



