I have custom class named Department, in which equals and hashCode are both overridden. Please find the snippet as below:
class Department {
private final int id;
private final String name;
private final int count;
public Department(int id, String name, int count) {
super();
this.id = id;
this.name = name;
this.count = count;
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (!(obj instanceof Department))
return false;
final Department emp = (Department) obj;
return emp.name != null && emp.name.equals(name) && emp.count == count && emp.id == id;
}
@Override
public int hashCode() {
return count + name.length();
}
@Override
public String toString() {
return "ID: " + id + ", Name: " + name + ", Age: " + count + ", hashCode: " + hashCode();
}
}
In the main method, I have initialized two departments in such a way that, their equals will return false but will have same hashcode. Those two departments are then added to a HashMap. Please find main method call as below:
public static void main(String[] args) {
final Department dep1 = new Department(1, "software", 35);
final Department dep2 = new Department(2, "software", 35);
System.out.println("\n\nIs dep1.equals(dep2)? -- " + dep1.equals(dep2));
System.out.println("Is dep1==dep2? -- " + (dep1 == dep2));
System.out.println("\n\nDepartment 1: " + dep1);
System.out.println("Department 2: " + dep2);
final HashMap<Department, String> departmentHashMap = new HashMap<>();
departmentHashMap.put(dep1, "Software 1");
System.out.println("\n\nDepartment 1 added to map");
System.out.println("Is Department 2 available in map? -- " + departmentHashMap.get(dep2));
System.out.println("Is Department 2 key available in map? -- " + departmentHashMap.containsKey(dep2));
departmentHashMap.put(dep2, "Software 2");
System.out.println("\n\nDepartment 1: " + departmentHashMap.get(dep1));
System.out.println("Department 2: " + departmentHashMap.get(dep2));
for (final Entry<Department, String> entry : departmentHashMap.entrySet()) {
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
}
As per the documents, when two different entries having same hashcode but not satisfying equals comparison, will cause collision in HashMap and entries will be stored as linked list. I did not observe this particular behavior. But when I iterated over the HashMap entries, they were fetched as individual entries, not linked list. Please find the output as below:
Is dep1.equals(dep2)? -- false
Is dep1==dep2? -- false
Department 1: ID: 1, Name: software, Age: 35, hashCode: 43
Department 2: ID: 2, Name: software, Age: 35, hashCode: 43
Department 1 added to map
Is Department 2 available in map? -- null
Is Department 2 key available in map? -- false
Department 1: Software 1
Department 2: Software 2
Key: ID: 1, Name: software, Age: 35, hashCode: 43, Value: Software 1
Key: ID: 2, Name: software, Age: 35, hashCode: 43, Value: Software 2
I could not reference exemplifying this particular case anywhere. Any help to clarify the concept will be highly appreciated.