gpt4 book ai didi

java - lambda 中使用的循环变量

转载 作者:行者123 更新时间:2023-11-29 08:36:16 25 4
gpt4 key购买 nike

Set<Malfunction> malfunctions= ...
for (final String guideword : guidewords) {
Malfunction malf = malfunctions.stream()
.filter(m -> m.getGuideword().equals(guideword))
.collect(Collectors.toList())
.get(0);

当我避免使用 guideword变量,但使用硬编码值,例如“some_reasonable_string”一切正常。

如果我使用 guideword from循环出现问题:有一个Set<Hazard>malfunctions 中应该填写,但在 malf 中为空。

最佳答案

为什么要混淆两个概念?迭代老派;但是然后使用流?

建议:使用纯流解决方案;喜欢:

final List<String> guidewords = Arrays.asList("A", "C");
Set<String> malfunctions = new HashSet<>(Arrays.asList("A", "B"));

System.out.println(malfunctions.stream().filter(e -> guidewords.contains(e)).collect(Collectors.toList()).get(0));

(简化您的输入以放下一个适用于所有人的示例;当然您必须对其进行调整以使用 contains(e.getGuideword()) 来满足您的要求)。

关于java - lambda 中使用的循环变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43933401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com