Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the differences between two Array Lists based on a property?

I have two array list. Each has list of Objects of type Employee.

The Employee class looks like below

    public class Employee {

    Employee(String firstname, String lastname, String employeeId) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.employeeId = employeeId;
    }

    private int id; // this is the primary key from employee table

    private String firstname;

    private String lastname;

    private String employeeId; // manually assigned unique id to each employee

    // getters and setters

}

I need to find the differences between the two lists based on a property of the employee object which is employee id.

Employee id is manually generated unique id given to each employee.

    import java.util.ArrayList;
import java.util.List;


public class FindDifferences {

    public static void main(String args[]){
        List<Employee> list1 = new ArrayList<Employee>(); 
        List<Employee> list2 = new ArrayList<Employee>(); 

        list1.add(new Employee("F1", "L1", "EMP01"));
        list1.add(new Employee("F2", "L2", "EMP02"));
        list1.add(new Employee("F3", "L3", "EMP03"));
        list1.add(new Employee("F4", "L4", "EMP04"));
        list1.add(new Employee("F5", "L5", "EMP05"));

        list2.add(new Employee("F1", "L1", "EMP01"));
        list2.add(new Employee("F2", "L2", "EMP02"));
        list2.add(new Employee("F6", "L6", "EMP06"));
        list2.add(new Employee("F7", "L7", "EMP07"));
        list2.add(new Employee("F8", "L8", "EMP08"));

        List<Employee> notPresentInList1 = new ArrayList<Employee>(); 
        // this list should contain EMP06, EMP07 and EMP08

        List<Employee> notPresentInList2= new ArrayList<Employee>(); 
        // this list should contain EMP03, EMP04 and EMP05



    }

}
like image 947
ashishjmeshram Avatar asked Jun 29 '12 11:06

ashishjmeshram


People also ask

How do you compare elements in two array lists?

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 you compare a field between two lists of objects?

Java equals() method This method accepts an object to be compared for equality with the list. It returns true if the specified object is equal to the list, else returns false. In the following example, we have create two ArrayList firstList and secondList. Comparing both list by using equals() method, it returns true.

How do you check if all elements in an ArrayList are equal Java?

distinct() Let's look at one particular solution making use of the distinct() method. If the count of this stream is smaller or equal to 1, then all the elements are equal and we return true.


1 Answers

Override equals() and hashcode() methods of your Employee class to only use employeeId when checking for equality (im not sure about why you need the id field. you might what to incorporate it as well). NetBeans / Eclipse IDEs can do this for you. Then you can create a copy of your original lists and use List.removeAll() to calculate difference.

like image 178
Andrew Butenko Avatar answered Oct 12 '22 09:10

Andrew Butenko