1
public class Employee{

int empId;
String empName;
List <EmployeeAddress> empAdd;

// getters & setters

}

public class EmployeeAddress{
String city;
String state;

// getters & setters

}

now I make two objects of employee class, and want to compare them. I have used list in the employee class since employee can have multiple addresses. Need help on this

davioooh
  • 23,742
  • 39
  • 159
  • 250
Sunny
  • 91
  • 1
  • 13
  • If you need to compare if objects are the same, so use `equals()` method: `employee1.equals(employee2)` will return `true` if they are equals, or both are null and false otherwise – TEXHIK Feb 18 '15 at 15:42
  • first question will be to define condition which objects are equal, then just implement `equals` and `hashCode` method relativly to your definition – user902383 Feb 18 '15 at 15:43

2 Answers2

1

Because is not clear what you mean by saying 'compare', I suppose you need to override equals method in your Employee class.

First of all I suggest you to take a look to this interesting question on SO: What issues should be considered when overriding equals and hashCode in Java?

Then, according to equals method contract for List interface:

two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.

So you could code something like:

@Override
public boolean equals(Object obj) {
    Employee e = (Employee) obj;
    return this.empId == e.empId && this.empName.equals(e.empName) && this.empAdd.equals(e.empAdd);
}

Or you can define your custom logic for list comparison...

Community
  • 1
  • 1
davioooh
  • 23,742
  • 39
  • 159
  • 250
0

First, I would suggest that it would be good to change your List<EmployeeAddress> to Set<EmployeeAddress>. This helps in two ways:

  1. It will avoid duplicate addresses in the list of addresses for each employee.
  2. It will make two employees equal without regard of the order in which addresses appear, since their is no order in a set.

Having said that, be sure that the equals method of EmployeeAddress is well implemented too, since the Set interface will require it to function properly when detecting duplicates. And then you would implement equals as:

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (!(obj instanceof Employee)) {
        return false;
    }
    Employee other = (Employee) obj;
    if (this.empAdd == null) {
        if (other.empAdd != null) {
            return false;
        }
    } else if (!this.empAdd.equals(other.empAdd)) {
        return false;
    }
    if (this.empId != other.empId) {
        return false;
    }
    if (this.empName == null) {
        if (other.empName != null) {
            return false;
        }
    } else if (!this.empName.equals(other.empName)) {
        return false;
    }
    return true;
}

The implementation of the equals method for the EmployeeAddress class should be as follows:

    @Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (!(obj instanceof EmployeeAddress)) {
        return false;
    }
    EmployeeAddress other = (EmployeeAddress) obj;
    if (this.city == null) {
        if (other.city != null) {
            return false;
        }
    } else if (!this.city.equals(other.city)) {
        return false;
    }
    if (this.state == null) {
        if (other.state != null) {
            return false;
        }
    } else if (!this.state.equals(other.state)) {
        return false;
    }
    return true;
}
Jadiel de Armas
  • 8,405
  • 7
  • 46
  • 62