How to find differences between two lists of arrays based on a property?

I have a list of arrays. Each has a list of objects of type Employee.

Employee class looks 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 the property of the employee object, which is the employee identifier.

The employee identifier assigns a unique identifier to each individual identifier.

    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



    }

}
+5
source share
4 answers

equals() hashcode() Employee, employeeId (im , id). ). IDE NetBeans/Eclipse . List.removeAll() .

+6

, ? . , . employeeId Collections.sort . . . XML, XMLUnit Diff class, . . , .

0

removeAll :

list1.removeAll(list2);

list1 list2, list1 , 2  EMP03  EMP04  EMP05

And override the equals method in the Employee class

     @Override
    public boolean equals(Object obj) {
        Employee employee = (Employee)obj;

        if ( this.employeeId.equalsIgnoreCase(employee.employeeId)){
            return true;
        }
        return false;

    }
0
source

Put both employee lists on cards. The key employeeId. Value is an object employee. Then use removeAllas @AndrewButenko suggested. You should use maps for more effective searches than listings. (Removing is related to the search.) I would recommend set, but then you will need to implement equalsand hashcode. They are already implemented for String.

Map<String, Employee> map1 = new HashMap<String, Employee>();
for (Employee e : list1) {
    map1.put(e.getEmployeeId(), e);
}
Map<String, Employee> map2 = new HashMap<String, Employee>();
for (Employee e : list2) {
    map2.put(e.getEmployeeId(), e);
}

// clone makes sure we don't mess with the original map2 because we will reuse it
Collection<Employee> notPresentInList1 = map2.clone().removeAll(map1).values();

Collection<Employee> notPresentInList2 = map1.removeAll(map2).values();

If you care about the order of the results, you can sort the collection at the end or use it TreeMapinstead.

0
source

All Articles