java - 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 reuse rootopt object inside of foreach. there way reuse variable? have following message "can not resolve symbol rootopt" when write rootopt.getchildoptions() inside foreach. please find below did: have tried rewrite loop below 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); (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); } } } } }); (option rootopt : opts) { (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); (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); } } } } } }
the scope of rootopt
ends @ closing parenthesis. write instead
opts.stream().foreach(rootopt -> rootopt.getchildoptions().stream().foreach(subopt -> { ... }); );
however streams not intended replace loops. more canonical way of using them 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 code though. refactoring several methods mike suggested making easier read.
Comments
Post a Comment