Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java lambda中使用的循环变量 设置故障=。。。 for(最终字符串引导词:引导词){ 故障故障=故障。流() .filter(m->m.getGuideword().equals(guideword)) .collect(收集器.toList()) .get(0);_Java_Lambda_Java 8_Closures_Java Stream - Fatal编程技术网

Java lambda中使用的循环变量 设置故障=。。。 for(最终字符串引导词:引导词){ 故障故障=故障。流() .filter(m->m.getGuideword().equals(guideword)) .collect(收集器.toList()) .get(0);

Java lambda中使用的循环变量 设置故障=。。。 for(最终字符串引导词:引导词){ 故障故障=故障。流() .filter(m->m.getGuideword().equals(guideword)) .collect(收集器.toList()) .get(0);,java,lambda,java-8,closures,java-stream,Java,Lambda,Java 8,Closures,Java Stream,当我避免使用guideword变量,而是使用硬编码的值,例如“some\u reasonal\u string”时,一切正常 如果我使用循环中的引导词,问题就会出现:有一个集合,它是并且应该在故障中填充,但随后在malf中是空的。为什么你混淆了两个概念?重复老派;然后使用流 建议:使用流式解决方案;例如: Set<Malfunction> malfunctions= ... for (final String guideword : guidewords) { Malfunc

当我避免使用
guideword
变量,而是使用硬编码的值,例如“some\u reasonal\u string”时,一切正常


如果我使用循环中的
引导词
,问题就会出现:有一个
集合
,它是并且应该在故障中填充,但随后在malf中是空的。

为什么你混淆了两个概念?重复老派;然后使用流

建议:使用流式解决方案;例如:

Set<Malfunction> malfunctions= ...
for (final String guideword : guidewords) {
    Malfunction malf = malfunctions.stream()
            .filter(m -> m.getGuideword().equals(guideword))
            .collect(Collectors.toList())
            .get(0);
final List guidewords=Arrays.asList(“A”、“C”);
Set=newhashset(Arrays.asList(“A”、“B”));
System.out.println(误动作.stream().filter(e->guidewords.contains(e)).collect(collector.toList()).get(0));

(简化您的输入以列出一个适用于所有人的示例;当然,您必须调整它以使用
包含(例如getGuideword())
,以符合您的要求)。

为什么您要混淆两个概念?重复老派;然后使用流

建议:使用流式解决方案;例如:

Set<Malfunction> malfunctions= ...
for (final String guideword : guidewords) {
    Malfunction malf = malfunctions.stream()
            .filter(m -> m.getGuideword().equals(guideword))
            .collect(Collectors.toList())
            .get(0);
final List guidewords=Arrays.asList(“A”、“C”);
Set=newhashset(Arrays.asList(“A”、“B”));
System.out.println(误动作.stream().filter(e->guidewords.contains(e)).collect(collector.toList()).get(0));

(简化您的输入以列出一个适用于所有人的示例;当然,您必须调整它以使用
包含(例如getGuideword())
,以符合您的要求)。

您无法从lambda访问循环变量,因为它不是最终的。一个解决方案是在循环中获取它的最终副本

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));

然而,正如GhostCat所指出的,这里可能有一个更基本的问题。

您无法从lambda访问循环变量,因为它不是最终的。一个解决方案是在循环中获取它的最终副本

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));

然而,正如GhostCat所指出的,这里可能还有一个更为根本的问题。

我想知道为什么他的代码不起作用;我忘记了最后一件事。所以,假设你在这方面是正确的……你得到了我的投票;-)不幸的是,对于最后一个副本,上面描述的问题仍然存在。他的循环变量是
f最后
,所以这不可能是问题所在。拥有一个临时的
最终
变量不会改变任何东西。我想知道为什么他的代码不起作用;我忘记了最后一件事。所以,假设你在这一部分是正确的……你得到了我的投票;-)同样,对于最终副本,不幸的是,上面描述的问题仍然存在变量是
final
,所以这不是问题所在。拥有一个临时
final
变量不会改变任何事情。那么这个
集是什么?你用它做什么?你能不能提供一个?搁置,而不是
collect()
get()
你可以只使用
findFirst().get()
(假设筛选后至少有一个元素)。那么这是什么
Set
以及您在用它做什么?您能否提供一个?旁白,而不是
collect()
get()
,您可以只使用
findFirst().get()
(假设筛选后至少有一个元素)。