2

I have 3 classes

Employee class is like

  class Employee {
    int id;
    String name;
    long salary;
    List<Address> address;
    //getter setter, equals and hashcode and parameterized constructor}

And My address class is like

public class Address {
int housenum;
String streetname;
int pincode;
//getter setter and parameterized constructor
}

and My Test class is like

 Address address1 = new Address(10, "str 1", 400043);
            Address address2 = new Address(10, "str 1", 400043);

            List<Address> addressLst1= new ArrayList<>();
            List<Address> addressLst2= new ArrayList<>();
            addressLst1.add(address1);
            addressLst1.add(address2);
            addressLst2.add(address1);
            addressLst2.add(address2);
            Employee employee1 = new Employee(1, "EMP1", 1000, addressLst1);
            Employee employee2 = new Employee(1, "EMP1", 1000, addressLst2);

            Set<Employee> set = new HashSet<>();
            set.add(employee1);
            set.add(employee2);

            System.out.println(":::::::::::::::addressLst1:::::::::" + addressLst1.hashCode());
            System.out.println(":::::::::::::::addressLst2:::::::::" + addressLst2.hashCode());

            System.out.println(":::::::::::::::address1:::::::::" + address1.hashCode());
            System.out.println(":::::::::::::::address2:::::::::" + address2.hashCode());

            System.out.println(":::::::::::::::employee1:::::::::" + employee1.hashCode());
            System.out.println(":::::::::::::::employee2:::::::::" + employee2.hashCode());

            set.forEach(System.out::println);

            System.out.println(":::::::::::::::size:::::::::" + set.size());

I am getting different hashcode for address objects as I have not overridden equals and hashcode. But why I am getting same hashcode for two different lists of addresses i.e. addressLst1 and addressLst2 And why I am getting the size of set as 1, why the hashcode of two employee object is the same? And what is correct way to override equals and hashcode for the custom object which consist of list of another custom object?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
implosivesilence
  • 536
  • 10
  • 24
  • https://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java?rq=1 – Shubham Mar 05 '20 at 06:27
  • Does this answer your question? [What issues should be considered when overriding equals and hashCode in Java?](https://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java) – Shababb Karim Mar 05 '20 at 06:29
  • This already answered here: https://stackoverflow.com/questions/17159881/default-implementation-of-hashcode-returns-different-values-for-objects-construc – AMA Mar 05 '20 at 06:34
  • `{address1,address2}.equals({address1,address2})` since `address1.equals(address1)` and `address2.equals(address2)`; and `address1.equals(address1)` since `address1 == address1` (same for `address2`) - it uses `Object.equals()`, which just uses `==` itself, since `Address` has no `equals() ` – user85421 Mar 05 '20 at 07:41
  • `employee1.equals(employe2)` since its `equals` and `hashCode` are implemented to compare/use its attributes – user85421 Mar 05 '20 at 07:45

1 Answers1

2

The two Lists addressLst1 and addressLst2 contain the exact same elements in the exact same order, so the contract of List's equals requires that these two Lists will be equal to each other:

boolean java.util.List.equals(Object o)

Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null :e1.equals(e2)).) In other words, 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.

It doesn't matter that Address doesn't override equals and hashCode, since the Lists contain references to the same objects. While address1 is not equal to address2, both Lists contain references to both address1 and address2 in the same order, so the Lists are equal.

For the Employee class, you wrote that you did override equals and hashCode, so I'm assuming two Employees are equal if all of their properties are equal. Therefore the two Employee instances you try to add to the Set are equal.

Community
  • 1
  • 1
Eran
  • 387,369
  • 54
  • 702
  • 768