java8 stream接口终端操作 anyMatch,allMatch,noneMatch
anyMatch:判断的条件里,任意一个元素成功,返回true
原来的代码:
boolean isInPort = false;
for (String key : ygzxzb.keySet()) {
if (detectMovement(laterPosition, ygzxzb.get(key))) {
//如果找到船在某个港口内,则将 isInPort 设置为 true 并退出循环
isInPort = true;
break;
}
}
优化之后的代码:
boolean isInPort = ygzxzb.values().stream().anyMatch(jwdxx -> detectMovement(laterPosition, jwdxx));
allMatch:判断条件里的元素,所有的都是,返回true
noneMatch:与allMatch相反,判断条件里的元素,所有的都不是,返回true