Java - Lambda Stream ForEach 循环中不能中止循环并退出??为何???
我们在工作中一定经常使用for循环遍历,若是想在某一个条件退出,直接ruturn即可;
private static void arrayListForEach(){
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
arrayList.add("D");
arrayList.add("E");
arrayList.add("F");
for (String ar: arrayList) {
System.out.println("ar = " + ar);
switch (ar) {
case "E":
System.out.println("array => " + ar);
return;
default:
break;
}
}
System.out.println("================== ");
for (int i = 0; i < arrayList.size(); i++) {
System.out.println("arr = " + arrayList.get(i));
if ("c".equalsIgnoreCase(arrayList.get(i))){
System.out.println("brr => " + arrayList.get(i));
return;
}
}
}
输出:(我们可以看到在循环的“E”的时候,return生效了,直接中止了循环,常规操作吧。。。)
ar = A
ar = B
ar = C
ar = D
ar = E
array => E
但是我们现在大多数在用java8中的lambda也就是Stream处理数据或者处理循环数据,这个时候我们要是还想用上面的方式中止循环就有问题了;
private static void arrayListForEach(){
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
arrayList.add("D");
arrayList.add("E");
arrayList.add("F");
/*for (String ar: arrayList) {
System.out.println("ar = " + ar);
switch (ar) {
case "E":
System.out.println("array => " + ar);
return;
default:
break;
}
}*/
arrayList.forEach( ar -> {
System.out.println("ar = " + ar);
switch (ar) {
case "E":
System.out.println("array => " + ar);
return;
default:
break;
}
});
System.out.println("================== ");
for (int i = 0; i < arrayList.size(); i++) {
System.out.println("arr = " + arrayList.get(i));
if ("c".equalsIgnoreCase(arrayList.get(i))){
System.out.println("brr => " + arrayList.get(i));
return;
}
}
}
这里不管是换成:
arrayList.stream().forEachOrdered
或者
arrayList.stream().forEach
效果都是一样的。。。
输出;(在遇到“E”的时候没有中止循环,且直接走完了整个方法,这个return为什么没有生效呢???)
ar = A
ar = B
ar = C
ar = D
ar = E
array => E
ar = F
==================
arr = A
arr = B
arr = C
brr => C
解决办法:(使用arrayList.stream().anyMatch这个函数来进行遍历即可)
anyMatch()里接收一个返回值为boolean类型的表达式,只要返回true就会终止循环,这样可以将业务逻辑写在返回判断结果前。
当然arrayList.stream().filter(ar -> { System.out.println("ar=" + ar); return ar.equals("E"); }).findAny(); 也可以的;
private static void arrayListForEach(){
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
arrayList.add("D");
arrayList.add("E");
arrayList.add("F");
/*for (String ar: arrayList) {
System.out.println("ar = " + ar);
switch (ar) {
case "E":
System.out.println("array => " + ar);
return;
default:
break;
}
}*/
arrayList.stream().anyMatch( ar -> {
System.out.println("ar = " + ar);
switch (ar) {
case "E":
System.out.println("array => " + ar);
return true;
default:
break;
}
return false;
});
System.out.println("================== ");
for (int i = 0; i < arrayList.size(); i++) {
System.out.println("arr = " + arrayList.get(i));
if ("c".equalsIgnoreCase(arrayList.get(i))){
System.out.println("brr => " + arrayList.get(i));
return;
}
}
}
输出:(我们的预期一样,遇到“E”就在此处中止循环,然后执行下一个任务,这样似乎更加适合处理现实项目中出现的情况)
ar = A
ar = B
ar = C
ar = D
ar = E
array => E
==================
arr = A
arr = B
arr = C
brr => C