如何使用Java 8转换这段代码?

huangapple go评论76阅读模式
英文:

How to transform this piece of code using Java 8?

问题

我正在尝试使用Java 8来转换这段代码。

private boolean hasOneFuelType(final List<PoiBE> pois) {
    return pois.stream().noneMatch(poiBE -> poiBE.getDetails().getSimpleRefueling().getTypes().size() > 1);
}

因此,在上面的代码中,如果在poi列表中至少有一个poi具有大于1的类型列表,我希望返回false,否则返回true。我尝试了以下代码,但显然不正确:

pois.stream().anyMatch(poiBE -> {
    if(poiBE.getDetails().getSimpleRefueling().getTypes().size() > 1) {
        return false;
    }
    return true;
});
return true;
英文:

I am trying to transform this piece of code using Java 8.

private boolean hasOneFuelType(final List&lt;PoiBE&gt; pois) {
        for(PoiBE poiBE: pois) {
            if(poiBE.getDetails().getSimpleRefueling().getTypes().size() &gt; 1) {
                return false;
            }
        }
        return true;
    }

So, in the code above I want to return false if in the list of pois has at least one poi that has a list of Types that is bigger then 1, otherwise I want to return true.

I tried this code, but apparently is not correct.

pois.stream().anyMatch(poiBE -&gt; {
    if(poiBE.getDetails().getSimpleRefueling().getTypes().size() &gt; 1) {
      return false;
    }
      return true;
});

return true;

答案1

得分: 3

noneMatch 如果流中的任何项与给定的谓词匹配,则返回false,否则返回true。

return pois.stream()
         .noneMatch(poiBE -> poiBE.getDetails().getSimpleRefueling().getTypes().size() > 1);
英文:

noneMatch returns false if any item in the stream matches the given predicate, and true otherwise.

return pois.stream()
         .noneMatch(poiBE -&gt; poiBE.getDetails().getSimpleRefueling().getTypes().size() &gt; 1);

答案2

得分: 3

你很接近了。

anyMatch 内部的 return 语句并不是从你的方法返回,而是从传递给 anyMatch 的 lambda 返回:

boolean anyHasMoreThanOneFuelType = pois.stream().anyMatch(poiBE -&gt; {
    if(poiBE.getDetails().getSimpleRefueling().getTypes().size() &gt; 1) {
        return true;
    }
    return false;
});

return !anyHasMoreThanOneFuelType;

当然,如果你将 anyMatch 替换为 noneMatch 并直接返回比较的布尔结果,就可以简化这段代码,不再需要不必要的 if

return pois.stream().noneMatch(
    poiBE -&gt; poiBE.getDetails().getSimpleRefueling().getTypes().size() &gt; 1);
英文:

You're close.

The return statements inside the anyMatch don't return from your method, but from the lambda passed to anyMatch:

boolean anyHasMoreThanOneFuelType = pois.stream().anyMatch(poiBE -&gt; {
    if(poiBE.getDetails().getSimpleRefueling().getTypes().size() &gt; 1) {
      return true;
    }
    return false;
});

return !anyHasMoreThanOneFuelType;

This of course can be simplified if you replace anyMatch with noneMatch and skip the unnecessary if by directly returning the boolean result of the comparison:

return pois.stream().noneMatch(
    poiBE -&gt; poiBE.getDetails().getSimpleRefueling().getTypes().size() &gt; 1);

答案3

得分: 2

你可以使用

return pois.stream()
           .noneMatch(poiBE -> poiBE.getDetails().getSimpleRefueling().getTypes().size() > 1);
英文:

You can use

return pois.stream()
         .noneMatch(poiBE -&gt; poiBE.getDetails().getSimpleRefueling().getTypes().size() &gt; 1);

答案4

得分: 2

return pois.stream().allMatch(
poiBE -> poiBE.getDetails().getSimpleRefueling().getTypes().size() <= 1);

英文:
return pois.stream().allMatch(
    poiBE -&gt; poiBE.getDetails().getSimpleRefueling().getTypes().size() &lt;= 1);

huangapple
  • 本文由 发表于 2020年8月5日 16:08:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63260926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定