Jade_Layyne
Jade_Layyne

Reputation: 21

How to reuse the variable used for for loop first level inside for loop second level in nested for loop

Someone can tell me how can I reuse rootOpt object inside of my forEach. Is there any way to reuse this variable? I have the following message "Can not resolve symbol rootOpt" when I write rootOpt.getChildOptions() inside my forEach. Please find below what I did: I have tried to rewrite the for loop below by using stream. Thank you

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

Upvotes: 0

Views: 148

Answers (1)

Tesseract
Tesseract

Reputation: 8139

The scope of rootOpt ends at the closing parenthesis. You could write it like this instead

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

However streams were not really intended to simply replace for loops. A more canonical way of using them would be something like this.

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

I didn't test that code though. Also refactoring it into several methods as mike suggested would help making it easier to read.

Upvotes: 3

Related Questions