8

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?

3
  • you can add object of Person in your defined arraylist, check ur code.
    – DjP
    Commented Feb 18, 2015 at 11:14
  • @Crazy sorry. I have updated my question.
    – SKK
    Commented Feb 18, 2015 at 11:25
  • Please don't down vote the question. I have posted a right question. Thanks...
    – SKK
    Commented Feb 18, 2015 at 11:30

4 Answers 4

20

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);
    }
}
2
  • Is there any way to do it without nested loops? Using Collections class or any other class to solve this. Commented Oct 26, 2016 at 9:03
  • where is linq when i need it :(
    – Phil
    Commented Jan 4, 2017 at 12:11
3

Look at the modifications to person class

public static class Person{

        //setters and getters

        @Override
        public boolean equals(Object other) {
            if (!(other instanceof Person)) {
                return false;
            }

            Person that = (Person) other;

            // Custom equality check here.
            return this.getId() == that.getId();
        }

    }

Have overridden equals(Object other)

then simply do this

for (Person person : arrayList1) {
            arrayList2.remove(person);
}

your answer is array list 2, it will only contain odd objects

1
  • 1
    I actually really like this method. You really should learn to format your answer a bit better though. Commented Jan 11, 2018 at 21:42
0

You should iterate through the shortest ArrayList you have, so check which list is shorter and then iterate through all the indexes in that list against every index in the other list.

(This is assuming you don't have any duplicates in either list. If you do, you might want to return a list of all indexes found.)

-1

arrayListChars=new ArrayList<>(); //[M,A,R,V,E,L]

arrayListAddChars=new ArrayList<>(); //[M,A,....coming values]

int count = 0;

for(int i=0;i

if(arrayListAddChars.get(i).equals(arrayListChars.get(i))){

    count++;
   }
    else
    {
     break;
    }
  }

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.