我是靠谱客的博主 痴情云朵,最近开发中收集的这篇文章主要介绍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 8 foreach,返回Java 8 forEach所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部