简体   繁体   中英

How to compare two different sizes List Objects Java

I have two Lists, List A and List B having different sizes. List A is getting parsed from file and List B is fetching data from the database.

class A{
    private String id;
    private String mobile;
}

class B{
    private String id;
    private String name;
    private String address;
    private String mobile;
    private String pincode;
}

Now I want to Compare both the list and want to remove the respective ids from List A which are having the same mobile number as ListB .

Tried below code

private List<A> compareList(List<A> listA, List<B> listB){
    List<A> temp = new ArrayList<>();
    for(A a : listA){
        for(B b : listB){
            if(a.getId().equals(b.getId()) && !a.getMobile().equals(b.getMobile())){
                temp.add(a);
            }
        }
    }
return temp;
}

Can someone please guide me?

Your method creates a new list, instead of removing items from the existing list. Assuming you actually want to remove items, this is one way you can do it with the Java 8 streams API: remove an item from listA if it has the same mobile as an item in listB:

listA.removeIf(a -> listB.stream()
                         .anyMatch(b -> Objects.equals(a.getMobile(), b.getMobile())));

The streams API in this case is a little difficult to read. Here's the same without using a stream:

for (B b : listB) {
    listA.removeIf(a -> Objects.equals(a.mobile, b.mobile));
}

You can use a flag for existence and add in temp if not exists then temp contains only those elements of A which do not exist in B

List<A> temp = new ArrayList<>();
for(A a : listA){
    boolean isExist = false;
    for(B b : listB){
        if(a.getId().equals(b.getId()) && a.getMobile().equals(b.getMobile())){
            isExist = true; // if exist in List of B
            break;
        }
    }
    if(!isExist){   // if not exist in B then add in list
        temp.add(a);
    }
}

Note: In question you say to compare only mobile number but in code you are comparing id also, remove id equal check if you don't want it

I have two Lists, List A and List B having different sizes. List A is getting parsed from file and List B is fetching data from the database.

class A{
    private String id;
    private String mobile;
}

class B{
    private String id;
    private String name;
    private String address;
    private String mobile;
    private String pincode;
}

Now I want to Compare both the list and want to remove the respective ids from List A which are having the same mobile number as ListB .

Tried below code

private List<A> compareList(List<A> listA, List<B> listB){
    List<A> temp = new ArrayList<>();
    for(A a : listA){
        for(B b : listB){
            if(a.getId().equals(b.getId()) && !a.getMobile().equals(b.getMobile())){
                temp.add(a);
            }
        }
    }
return temp;
}

Can someone please guide me?

I think you should keep the mobiles separately, as they can probably be repeated. And after that compare the main collection with this list of mobiles.

private static List<A> compareList(List<A> listA, List<B> listB) {
    List<String> mobiles = listB.stream()
            .map(B::getMobile)
            .distinct()
            .collect(Collectors.toList());

    return listA.stream()
            .filter(entity -> !mobiles.contains(entity.getMobile()))
            .collect(Collectors.toList());
}

To create new filtered List

Collect all element in new list if condition does not match

List<A> filteredList =
        aList.stream()
            .filter(Predicate.not(a -> bList.stream().anyMatch(b -> a.getId().equals(b.getId()) && a.getMobile().equals(b.getMobile()))))
            .collect(Collectors.toList());

For in place replacement

aList.removeIf(a -> bList.stream().anyMatch(b -> a.getId().equals(b.getId()) && a.getMobile().equals(b.getMobile())));   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM