0

I have this following code:

class EmployeeDetails {
    private String designation;
    private int experience;
    
    public EmployeeDetails(String designation, int experience) {
        this.designation = designation;
        this.experience = experience;
    }

    @Override
    public boolean equals(Object obj) {
        if(!(obj instanceof EmployeeDetails)) return false;
        
        EmployeeDetails employeeDetails = (EmployeeDetails)obj;
        
        return this.designation.equals(employeeDetails.designation) && this.experience == employeeDetails.experience;
    }

    @Override
    public String toString() {
        return "EmployeeDetails [designation=" + designation + ", experience=" + experience + "]";
    }
    
}

class Employee {
    private int id;
    private String name;
    private EmployeeDetails employeeDetails;
    
    public Employee(int id, String name, EmployeeDetails employeeDetails) {
        this.id = id;
        this.name = name;
        this.employeeDetails = employeeDetails;
    }

    @Override
    public boolean equals(Object obj) {
        if(!(obj instanceof Employee)) return false;
        
        Employee employee = (Employee)obj;
        
        if(this.id == employee.id && this.name.equals(employee.name) && this.employeeDetails.equals(employee.employeeDetails)) return true;
        
        return false;
        
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }

    @Override
    public String toString() {
        return "Employee [id=" + id + ", name=" + name + ", employeeDetails=" + employeeDetails + "]";
    }
}

public class Test {
    
    public static void main(String[] args) {
        Set<Employee> s1 = new HashSet<>();

        s1.add(new Employee(11, "John", new EmployeeDetails("Java Developer", 2)));
        s1.add(new Employee(15, "Bob", new EmployeeDetails("Angular Developer", 1)));
        
        Set<Employee> s2 = new HashSet<>();
        s2.add(new Employee(11, "John", new EmployeeDetails("Java Developer", 2)));
        s2.add(new Employee(15, "Bob", new EmployeeDetails("Angular Developer", 1)));
        
        System.out.println("Set 1: " + s1);
        System.out.println("Set 2: " + s2);
        
        System.out.println(s1.equals(s2)); // line 1
    }
}

My requirement is to compare two sets s1 and s2 to check if they contain the same Employee information. Both s1 and s2 are Sets of Employee objects. I found in this thread "https://stackoverflow.com/questions/6187294/java-set-collection-override-equals-method" that HashSet not just calls the equals method on the containing object but also checks the hash code of those containing objects to compare for equality. Therefore I have overridden that hashcode() in Employee class.

My question is that:

If it is enough to return the hashcode of the member object "name" in Employee

or

Should I do a bitwise xor of the hashcode of "name" and "employeeDetails" by having a hashcode() in EmployeeDetails that returns the "designation" hashcode from there. i.e. I do something like this.

In Employee:

    @Override
    public int hashCode() {
        return name.hashCode()^employeeDetails.hashCode();
    }

In EmployeeDetails:

    @Override
    public int hashCode() {
        return designation.hashCode();
    }

In short, I want to ask what kind of hash code strategy should I use here in the hascode() in the Employee class. And ultimately I want to just compare the sets for the values in them.

Shri
  • 109
  • 9

0 Answers0