如何在嵌套for循环中重用用于for循环第一级的变量for循环第二级

时间:2017-04-14 13:17:01

标签: java

有人可以告诉我如何在forEach中重用rootOpt对象。有没有办法重用这个变量?我有以下消息"无法解析符号rootOpt"当我在forEach中编写rootOpt.getChildOptions()时。请在下面找到我做的: 我试过用stream重写下面的for循环。谢谢

opts.stream()
                .flatMap(rootOpt -> rootOpt.getChildOptions().stream())
                .forEach(subOpt -> {
                    if (subOpt.getOptLogic() != null && subOpt.getOptLogic().getCant() != null && !"".equals(subOpt.getOptLogic().getCant())) {
                        String[] oldCHs = subOpt.getOptLogic().getCant().split("( )");
                        OptionList samePriceSibs = getSamePriceS(rootOpt.getChildOptions(), subOpt);
                        for (String ch : oldCHs) {
                            Option chRootOpt = childOptCodeToParentOptMap.get(ch.toUpperCase());
                            if (chRootOpt != null) {
                                if (!DoesVariableOptionsCompletelyExcludeOther(samePriceSibs, chRootOpt.getChildOptions())) {
                                    List<OptionList> tmp = new ArrayList<OptionList>();
                                    tmp.add(samePriceSibs);
                                    tmp.add(chRootOpt.getChildOptions());
                                    optionsPairsToRemoveCHs.add(tmp);
                                }
                            }
                        }
                    }
                });

            for (Option rootOpt : opts) {
                for (Option subOpt : rootOpt.getChildOptions()) {
                    if (subOpt.getOptLogic() != null && subOpt.getOptLogic().getCant() != null && !"".equals(subOpt.getOptLogic().getCant())) {
                        String[] oldCHs = subOpt.getOptLogic().getCant().split("( )");
                        OptionList samePriceSibs = getSamePriceS(rootOpt.getChildOptions(), subOpt);
                        for (String ch : oldCHs) {
                            Option chRootOpt = childOptCodeToParentOptMap.get(ch.toUpperCase());
                            if (chRootOpt != null) {
                                if (!DoesVariableOptionsCompletelyExcludeOther(samePriceSibs, chRootOpt.getChildOptions())) {
                                    List<OptionList> tmp = new ArrayList<OptionList>();
                                    tmp.add(samePriceSibs);
                                    tmp.add(chRootOpt.getChildOptions());
                                    optionsPairsToRemoveCHs.add(tmp);
                                }
                            }
                        }
                    }
                }
            }

1 个答案:

答案 0 :(得分:3)

rootOpt的范围在右括号处结束。 你可以这样写它

opts.stream().forEach(rootOpt ->
  rootOpt.getChildOptions().stream().forEach(subOpt -> {
    ...
  });
);

然而,流并不是真正意图简单地替换循环。使用它们的更规范的方式就是这样。

Stream<List<OptionList>> optionsPairsToRemoveCHs = opts.stream()
  .flatMap(rootOpt ->
    rootOpt.getChildOptions().stream()
           .filter(subOpt -> subOpt.getOptLogic() != null && subOpt.getOptLogic().getCant() != null && !"".equals(subOpt.getOptLogic().getCant()))
           .flatMap(subOpt -> {
              String[] oldCHs = subOpt.getOptLogic().getCant().split("( )");
              OptionList samePriceSibs = getSamePriceS(rootOpt.getChildOptions(), subOpt);
              return Stream.of(oldCHs)
                           .map(ch -> childOptCodeToParentOptMap.get(ch.toUpperCase()))
                           .filter(chRootOpt -> chRootOpt != null && !DoesVariableOptionsCompletelyExcludeOther(samePriceSibs, chRootOpt.getChildOptions()))
                           .map(chRootOpt -> Arrays.asList(samePriceSibs, chRootOpt.getChildOptions()));
           })
  );

我没有测试那段代码。同样,将其重构为mike建议的几种方法将有助于使其更容易阅读。