I have used for and if loop to find intersections of list by comparing its inners JSON. I am looking for the solution using CollectionUtils or Java 8 or some other similar solution.
private List<IBXLocation> compareIbxLocationDetails(List<IBXLocation> serviceIbxsForLoggedInUser,
List<IBXLocation> serviceIbxsForUser) {
List<IBXLocation> finalList=new ArrayList();
for (IBXLocation ibxForLoggedInUser : serviceIbxsForLoggedInUser) {
String ibxSelected=ibxForLoggedInUser.getIbx();
boolean ibxFound = false;
ibxLoop:for (IBXLocation permittedIBXForUser : serviceIbxsForUser) {
if (ibxSelected.equals(permittedIBXForUser.getIbx())) {
IBXLocation newIbx = new IBXLocation(ibxSelected);
List<Cage> newCageList=new ArrayList();
if (!CollectionUtils.isEmpty(ibxForLoggedInUser.getCageDetails())) {
for (Cage selectedCage : ibxForLoggedInUser.getCageDetails()) {
String loggedInSelectedCageStr = selectedCage.getCage();
for (Cage permittedCage : permittedIBXForUser.getCageDetails()) {
if (loggedInSelectedCageStr.equals(permittedCage.getCage())) {
newCageList.add(permittedCage);
}
}
newIbx.setCageDetails(newCageList);
}
finalList.add(newIbx);
}
ibxFound = true;
break ibxLoop;
}
}
}
return finalList;
}
newIbx.setCageDetails(newCageList);is probably a bug. It will keep overwriting the value. You should probably move it outside the loop to right beforefinalList.add(newIbx);