i need to loop through few for loops, to identify the correct name(nm), based on the matching keys, i need to add the list to the parent list.
here is the code. the first one will loop through all RM’s , for each of the RM, we have NM list, for each for NM object, need to find from final list if key matches. if so add that NM to RM list as well as set some sequence number. this code working as expected, but it is using multiple for loops.
i tried to decrease the complexity, but i am unable to do so.
for (RM rm : con.getCt().getRms()) {
for (NM nameLst : rm.getNms()) {
List<NM> nmToAdd = new ArrayList<>();
for (NM finalLst : finalList) {
if (finalLst.getKey().equals(nameLst.getKey())) {
nmToAdd.add(finalLst);
rm.setSqn(finalLst.getSqn());
}
rm.setNames(namesToAdd);
}
}
}
is it possible to decrease the complexity or use java streams to achieve the above requirement? any pointers would be helpful.
Thanks.
above code is working as expected but i feel, the complexity is high. want to see if we can do it through java streams or any other way, but i could not come up with any solution. so looking for ideas.