/** * CSE 373, Spring 2011, Jessica Miller * This class represents information about an employee. We used this class to * see that if we only override Object's equals method, HashSet does not work * as expected for instances of Employee that have the same value for fields. * We then overrode the hashCode method according to the guidelines discussed * in class. This ensured HashSet did not insert two instances of Employee * that were duplicates (i.e. had the fields with the exact same values). */ public class Employee { private String name; private int salary; private int deptID; public Employee(String name, int salary, int deptID) { this.name = name; this.salary = salary; this.deptID = deptID; } public boolean equals(Object o) { if (o == this) { return true; } if (!(o instanceof Employee)) { return false; } Employee e = (Employee)o; return (name.equals(e.name)) && (salary == e.salary) && (deptID == e.deptID); } public int hashCode() { int result = 17; result = 37 * result + this.name.hashCode(); result = 37 * result + this.salary; return 37 * result + this.deptID; } }