英文:
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<PoiBE> pois) {
        for(PoiBE poiBE: pois) {
            if(poiBE.getDetails().getSimpleRefueling().getTypes().size() > 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 -> {
    if(poiBE.getDetails().getSimpleRefueling().getTypes().size() > 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 -> poiBE.getDetails().getSimpleRefueling().getTypes().size() > 1);
答案2
得分: 3
你很接近了。
anyMatch 内部的 return 语句并不是从你的方法返回,而是从传递给 anyMatch 的 lambda 返回:
boolean anyHasMoreThanOneFuelType = pois.stream().anyMatch(poiBE -> {
    if(poiBE.getDetails().getSimpleRefueling().getTypes().size() > 1) {
        return true;
    }
    return false;
});
return !anyHasMoreThanOneFuelType;
当然,如果你将 anyMatch 替换为 noneMatch 并直接返回比较的布尔结果,就可以简化这段代码,不再需要不必要的 if:
return pois.stream().noneMatch(
    poiBE -> poiBE.getDetails().getSimpleRefueling().getTypes().size() > 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 -> {
    if(poiBE.getDetails().getSimpleRefueling().getTypes().size() > 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 -> poiBE.getDetails().getSimpleRefueling().getTypes().size() > 1);
答案3
得分: 2
你可以使用
return pois.stream()
           .noneMatch(poiBE -> poiBE.getDetails().getSimpleRefueling().getTypes().size() > 1);
英文:
You can use
return pois.stream()
         .noneMatch(poiBE -> poiBE.getDetails().getSimpleRefueling().getTypes().size() > 1);
答案4
得分: 2
return pois.stream().allMatch(
poiBE -> poiBE.getDetails().getSimpleRefueling().getTypes().size() <= 1);
英文:
return pois.stream().allMatch(
    poiBE -> poiBE.getDetails().getSimpleRefueling().getTypes().size() <= 1);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论