'Iterate over multiple list in java
While i am trying to convert below code snippet using java8 getting "Local variable sId defined in an enclosing scope must be final or effectively final" error.
Existing code:
String sId = "";
String uId = "";
FinalResp resp = new FinalResp();
List<LiqResp> liqRespList = getResp.getLiqRespList();
for(LiqResp liqResp : liqRespList){
List<ListOfAcq> listAcq = liqResp.getLiqList();
for(ListOfAcq acqList : listAcq){
List<Acq> acqs = acqList.getAcqList();
for(Acq acq :acqs){
if(sId.equalIgnoreCase(acq.getId()) || uId.equalIgnoreCase(acq.getId())){
resp.setId(acqList.getId());
}
}
}
}
return resp;
Below is the snippet which i tried to convert using java8:
String sId = "";
String uId = "";
FinalResp resp = new FinalResp();
List<LiqResp> liqRespList = getResp.getLiqRespList();
liqRespList.parallelStream.forEach(liqResp -> liqResp.getLiqList().parallelStream.
forEach(acqList->acqList.getAcqList().stream.map(acq -> {
return conversion(acq, acqList, sId,uId,resp); //getting error here as "Local variable sId
//defined in an enclosing scope must be final
// or effectively final"
})));
private FinalResp conversion(Acq acq, ListOfAcq acqList, String sId,String uId,FinalResp resp){
if(sId.equalIgnoreCase(acq.getId()) || uId.equalIgnoreCase(acq.getId())){
resp.setId(acqList.getId());
}
return resp;
}
Performance wise is this correct approach.thanks in advance for help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|