概述
Boolean isSuccess = true;
if(aMap.size() != bMap.size())
{
return false;
}
aMap.entrySet().forEach(entry -> {
AKey aKey = entry.getKey();
BValue bValue = bMap.get(aKey);
if(bValue == null)
return;
AValue aValue = entry.getValue();
if(!aValue.getClosed().equals(bValue.getClosed()))
return;
if(!aValue.getClosedToArrival().equals(bValue.getClosedToArrival()))
return;
if(!aValue.getClosedToDeparture().equals(bValue.getClosedToDeparture()))
return;
if(!aValue.getLengthOfStayArrival().equals(bValue.getLengthOfStayArrival()))
return;
});
return isSuccess;
How can i return false when validation failure?
i tried to add return false, such as below:
if(!aValue.getLengthOfStayArrival().equals(bValue.getLengthOfStayArrival()))
return false;
but it is unexpected expression, who can help me have a look?
解决方案
You can't return false because you are in a lambda expression which implement the Consumer functional interface which method is of void type.
Instead, use anyMatch or noneMatch or allMatch :
return aMap.entrySet().stream().anyMatch(entry -> {
return false;// Put your condition here
});
I would also recommend to extract the validation in a method so that your pipeline looks like this :
return aMap.entrySet()
.stream()
.anyMatch(this::checkIfMatch);
Most of the times when opening {}, it's a good sign that you should create a new method.
最后
以上就是痴情云朵为你收集整理的java 8 foreach,返回Java 8 forEach的全部内容,希望文章能够帮你解决java 8 foreach,返回Java 8 forEach所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复