Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Compare two ArrayList of objects and find unmatching ids from the second ArrayList

I want to compare two ArrayList of objects and find the unmatching values from the second ArrayList based on the ids in the object.

For Example:

Person.java

private int id;
private String name;
private String place;

MainActivity.java:

ArrayList<Person> arrayList1 = new ArrayList<Person>();
arrayList1.add(new Person(1,"name","place"));
arrayList1.add(new Person(2,"name","place"));
arrayList1.add(new Person(3,"name","place"));

ArrayList<Person> arrayList2 = new ArrayList<Person>();
arrayList2.add(new Person(1,"name","place"));
arrayList2.add(new Person(3,"name","place"));
arrayList2.add(new Person(5,"name","place"));
arrayList2.add(new Person(6,"name","place"));

I want to compare the arrayList1, arrayList2 and need to find the unmatching values from the arrayList2. I need the id values 5,6.

How can I do this?

like image 399
SKK Avatar asked Feb 18 '15 11:02

SKK


People also ask

How do you compare two Arraylists that are having the same object?

You can compare two array lists using the equals() method of the ArrayList class, this method accepts a list object as a parameter, compares it with the current object, in case of the match it returns true and if not it returns false.

How do you compare two Arraylists?

The ArrayList. equals() is the method used for comparing two Array List. It compares the Array lists as, both Array lists should have the same size, and all corresponding pairs of elements in the two Array lists are equal. Parameters: This function has a single parameter which is an object to be compared for equality.

How do I compare two fields of objects in Java?

reflect. Field is used to compare two field objects. This method compares two field objects and returns true if both objects are equal otherwise false. The two Field objects are considered equal if and only if when they were declared by the same class and have the same name and type.

How do you compare two lists of strings in Java?

Java contentEquals() method This method accepts StringBuffer as a parameter to compare against the String. It returns true if the String represents the same sequence of characters as the specified StringBuffer, else returns false. In this example, we have created two ArrayList firstList and secondList of String type.


1 Answers

You can use an inner loop, to check if the Person's id from arrayList2 corresponds to any Person id in the arrayList1. You'll need a flag to mark if some Person was found.

ArrayList<Integer> results = new ArrayList<>();

// Loop arrayList2 items
for (Person person2 : arrayList2) {
    // Loop arrayList1 items
    boolean found = false;
    for (Person person1 : arrayList1) {
        if (person2.id == person1.id) {
            found = true;
        }
    }
    if (!found) {
        results.add(person2.id);
    }
}
like image 156
Simas Avatar answered Oct 23 '22 09:10

Simas