我是靠谱客的博主 痴情云朵,这篇文章主要介绍java 8 foreach,返回Java 8 forEach,现在分享给大家,希望可以做个参考。

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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(103)

评论列表共有 0 条评论

立即
投稿
返回
顶部