文章目录
-
-
- 概述
- 普通循环
-
- fori循环
- for循环
- forEach循环
-
- Iterable.forEach循环 —— by MutableBoolean
- Stream.forEach —— by anyMatch
- Stream.forEach —— by findAny
- 附录
-
- MutableBoolean
- 参考
-
概述
我们都知道continue、break用在哪里,怎么用以及用来干啥,即
continue
-
用于循环体内
-
用来结束本次循环
break -
用于循环体内
-
用来跳出循环
但是,forEach有些特殊,上面的关键字并不能在forEach循环体内直接用,否则会编译不通过,具体报错为
break outside switch or loop或
continue outside of loop
普通循环
fori循环
collection、map、Map.Entry的循环也类似,这里不再赘述
//example 1
int[] intArr = new int[]{1,2,3,4,5,6,7,8,9,10};
StringBuilder content = new StringBuilder(16);
for (int i = 0; i < intArr.length; i++) {
int item = intArr[i];
//跳出本次循环,继续下一轮循环
if(item == 5){ continue;}
//结束循环
if(item == 8){ break;}
content.append(item);
}
Assert.assertEquals("哦豁,写bug了吧","123467",content.toString());
for循环
collection、map、Map.Entry的循环也类似,这里不再赘述
//example2
int[] intArr = new int[]{1,2,3,4,5,6,7,8,9,10};
StringBuilder content = new StringBuilder(16);
for (int item : intArr) {
//跳出本次循环,继续下一轮循环
if(item == 5){ continue;}
//结束循环
if(item == 8){ break;}
content.append(item);
}
Assert.assertEquals("哦豁,写bug了吧","123467",content.toString());
forEach循环
Iterable.forEach循环 —— by MutableBoolean
//example 3
//注1 用continue会报错,并提示:continue outside of loop
//注2 用break会报错,并提示:break outside switch or loop
//注1 return实现continue的效果
//注2 用try-catch实现break效果
Collection<Integer> c = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
StringBuilder content = new StringBuilder(16);
try {
c.forEach((item) -> {
if (item == 5) {return;}
if (item == 8) {
throw new RuntimeException("不用再继续循环了");
}else{
content.append(item);
}
});
} catch (RuntimeException ex) {
}
Assert.assertEquals("哦豁,写bug了吧","123467",content.toString());
用try-catch这种抛异常的方式来跳出循环确实太挫了(代码不简洁不美观、且使用场景不太对,毕竟异常是意料之外,而这个跳出循环是正常的一种程序应用场景),在看看下面这种MutableBoolean方式
//example 4
//collection: MutableBoolean来实现break功能; return实现continue
//注1 MutableBoolean的定义方式见附录
Collection<Integer> c = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
StringBuilder content = new StringBuilder(16);
final MutableBoolean mutableBoolean = new MutableBoolean(true);
c.forEach((item) -> {
if (item == 5) {return;}
if (item == 8) {
mutableBoolean.setFlag(Boolean.FALSE);
}else if(mutableBoolean.getFlag()) {
content.append(item);
}
});
Assert.assertEquals("哦豁,写bug了吧","123467",content.toString());
see 是不是elegant、简洁多了。在看看Map的方式,其实也是一模一样的
StringBuilder content = new StringBuilder(16);
final MutableBoolean mutableBoolean = new MutableBoolean(true);
Map<Integer,Integer> map = new HashMap<>(16);
for (int i = 0; i <10 ; i++) {
map.put(i+1,i+1);
}
map.forEach((k,v)->{
if (v == 5) {return;}
if (v == 8) {
mutableBoolean.setFlag(Boolean.FALSE);
}else if(mutableBoolean.getFlag()) {
content.append(v);
}
});
Assert.assertEquals("哦豁,写bug了吧","123467",content.toString());
Stream.forEach —— by anyMatch
一种是anyMatch
//example 6
//stream: anyMatch跳出循环; return false 实现continue
Collection<Integer> c = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
StringBuilder content = new StringBuilder(16);
c.stream().anyMatch((item)->{
if (item == 5) {return false;}
boolean flag = 8 == item;
if(!flag) {
content.append(item);
}
return flag;
}) ;
Assert.assertEquals("哦豁,写bug了吧","123467",content.toString());
Stream.forEach —— by findAny
//example 7
//stream: findAny跳出循环; return false 实现continue
Collection<Integer> c = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
StringBuilder content = new StringBuilder(16);
c.stream().filter((item)->{
if (item == 5) {return false;}
boolean flag = 8 == item;
if(!flag) {
content.append(item);
}
return flag;
}).findAny() ;
Assert.assertEquals("哦豁,写bug了吧","123467",content.toString());
附录
MutableBoolean
/**
* 可变布尔变量
*/
static class MutableBoolean{
//标记
private Boolean flag;
public MutableBoolean(Boolean flag) {
this.flag = flag;
}
public Boolean getFlag() {
return flag;
}
public void setFlag(Boolean flag) {
this.flag = flag;
}
}
参考
- Java8中的foreach跳出循环break/return
相关文章
暂无评论...