0

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;
}
2
  • The line newIbx.setCageDetails(newCageList); is probably a bug. It will keep overwriting the value. You should probably move it outside the loop to right before finalList.add(newIbx); Commented Jul 12, 2018 at 20:12
  • This code is working as expected, I am looking for solution using collectionUtils or Java 8 for same code. Commented Jul 12, 2018 at 21:09

3 Answers 3

1

You can use this

Set ibxForLoggedInUserToSet= new HashSet<IBXLocation>(ibxForLoggedInUser);

for(IBXLocation per: serviceIbxsForUser){
     if (ibxForLoggedInUserToSet.contains(per)){
          finalList.add(per);
     }
}
Sign up to request clarification or add additional context in comments.

Comments

0

In org.apache.commons.collections4.ListUtils there is a method public static <E> List<E> intersection(List<? extends E> list1, List<? extends E> list2)

Comments

0

You can utilize Set to get intersections:

public <T> intersect(List<T> firstList, List<T> secondList) {
     Set<T> result = new HashSet<>();
     result.addAll(firstList);
     result.addAll(secondList);
     return result;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.