Take the 2-minute tour ×

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

The Employee class looks like below

    public class Employee {

    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

    private float fte;

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

    // getters and setters

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

I need to find the common employees between the two lists based on a employee id which have different fte's.

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

public class FindFTEDifferencesBetweenMatchingEmployeeIds {

    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", 1));
        list1.add(new Employee("F2", "L2", "EMP02", 1));
        list1.add(new Employee("F3", "L3", "EMP03", 1));
        list1.add(new Employee("F4", "L4", "EMP04", 1));
        list1.add(new Employee("F5", "L5", "EMP05", 1));
        list1.add(new Employee("F9", "L9", "EMP09", 0.7F));

        list2.add(new Employee("F1", "L1", "EMP01", 0.8F));
        list2.add(new Employee("F6", "L6", "EMP06", 1));
        list2.add(new Employee("F7", "L7", "EMP07", 1));
        list2.add(new Employee("F8", "L8", "EMP08", 1));
        list2.add(new Employee("F9", "L9", "EMP09", 1));

        List<FTEDifferences> commonInBothListWithDifferentFTE = new ArrayList<FTEDifferences>();
        // this should contain EMP01 and EMP09
        // since EMP02 has same FTE in both lists, it is ignored. 


Employees with employee id EMP01 and EMP09 are common in both lists and they also have different ftes in each list.

So, I would want to have another list which contains these two employees.

    public class FTEDifferences {

    private Employee fromList1;

    private Employee fromList2;

    public Employee getFromList1() {
        return fromList1;

    public void setFromList1(Employee fromList1) {
        this.fromList1 = fromList1;

    public Employee getFromList2() {
        return fromList2;

    public void setFromList2(Employee fromList2) {
        this.fromList2 = fromList2;

Please help.

PS. Even though it will be easy to do this in SQL, I can not do it in SQL query as I can not modify the queries. I only need to work with the two given lists. :(

share|improve this question
Just use a doubly nested loop to compare and find. –  nhahtdh Jul 19 '12 at 6:53
implement hashcode and equals on employees –  NimChimpsky Jul 19 '12 at 6:54
@nhahtdh. But the lists will be huge. About 500 employees. –  anything Jul 19 '12 at 6:54
500 objects is far from huge –  NimChimpsky Jul 19 '12 at 6:55
Possible duplicate of stackoverflow.com/questions/3163293/… –  sreehari Jul 19 '12 at 6:59
add comment

4 Answers

up vote 0 down vote accepted

Override equals method like below.

public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Employee other = (Employee) obj;
    if (employeeId == null) {
        if (other.employeeId != null)
            return false;
    } else if (!employeeId.equals(other.employeeId))
        return false;
    if (firstname == null) {
        if (other.firstname != null)
            return false;
    } else if (!firstname.equals(other.firstname))
        return false;
    if (Float.floatToIntBits(fte) == Float.floatToIntBits(other.fte))
        return false;
    if (lastname == null) {
        if (other.lastname != null)
            return false;
    } else if (!lastname.equals(other.lastname))
        return false;
    return true;

Then Create a finalList list and add list1 to it. Then call retainAll on finalList list with list2 which will give you common Employees based on employeeId

   List<Employee> finalList=new ArrayList<Employee>();
    List<Employee> commonInBothListWithDifferentFTE=finalList;


[Employee [firstname=F1, lastname=L1, employeeId=EMP01, fte=1.0], Employee [firstname=F9, lastname=L9, employeeId=EMP09, fte=0.7]]
share|improve this answer
The time complexity is not better than doubly nested loop. It is now hidden in retainAll method of List. –  nhahtdh Jul 19 '12 at 7:57
add comment

add equals(), hashcode() and compareTo() methods to your Employee class. Then you can try and to set operations, such as retainAll() or removeAll() from Collections static class.

It is good practice to add these methods to classes if they are to be compared in any way at any point.

share|improve this answer
add comment

Override hashcode and equals() method

share|improve this answer
add comment

If the maximum number of Employee is around 500 or even 1000, just use a doubly nested loop. It should be fast enough. The time complexity of this method is O(mn) where m and n are the sizes of the 2 lists.

If you expect the maximum number of Employee to be 3000 or even more, you can consider these 2 methods:

Another way is to sort both lists based on lexicographic order of employee number (implement a Comparator), and use a single loop to loop through both lists at the same time. The complexity is O(mlog m + nlog n).

If you want to be more optimal, you can use HashSet (overrides equals and hashCode) and put all the elements of one list in the HashSet. You can now loop through the other list and pick out the same employee from the first list and do the comparison. The amortized complexity is O(m + n).

share|improve this answer
add comment

Your Answer


