我的Java流的过滤器不起作用。

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

The Filter of my Java- Stream does not work

问题

我无法解释为什么我的过滤器不起作用。我在if语句中排除了条件,以检查条件是否真的为真,它确实为真。当DefaultLangv.getLinkDefaultLang())和currentLangv.getLinkCurrentLang())为Null时,我不想在我的List中保留该值!

manualResponseTOs.values().stream()
                          .filter(v -> isEmpty(v.getLinkDefaultLang()) && isEmpty(v.getLinkCurrentLang()))
                          .collect(Collectors.toList());
英文:

I can´t explain why my filter does not work. I exclude the condition in an if-statement to check that the condition is really true and it is. I don´t want to have the value in my List when the DefaultLang (v.getLinkDefaultLang()) and the currentLang (v.getLinkCurrentLang()) are Null!

manualResponseTOs.values().stream()
                          .filter(v -> isEmpty(v.getLinkDefaultLang()) && isEmpty(v.getLinkCurrentLang()))
                          .collect(Collectors.toList());

答案1

得分: 1

谢谢!现在我有了解决方案。

filter(v -> !(isEmpty(v.getLinkDefaultLang()) && isEmpty(v.getLinkCurrentLang())))
英文:

Thanks! Now I have the solution.

filter(v-> !(isEmpty(v.getLinkDefaultLang()) && isEmpty(v.getLinkCurrentLang())))

答案2

得分: 0

你还可以根据条件进行过滤,而不是将所有条件都放在同一行,如果你愿意的话。这样更容易阅读。

manualResponseTOs.values().stream()
        .filter(v -> !isEmpty(v.getLinkDefaultLang()))
        .filter(v -> !isEmpty(v.getLinkCurrentLang()))
        .collect(Collectors.toList());
英文:

You can also filter per condition as opposed to having all conditions on the same line if you like. It's a bit more readable.

manualResponseTOs.values().stream()
        .filter(v -> !isEmpty(v.getLinkDefaultLang()))
        .filter(v -> !isEmpty(v.getLinkCurrentLang()))
        .collect(Collectors.toList());

huangapple
  • 本文由 发表于 2020年10月2日 17:16:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64169059.html
匿名

发表评论

匿名网友

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

确定