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内容请搜索靠谱客的其他文章。
发表评论 取消回复