I had a very basic doubt with respect to Java HashMap when duplicate keys are being inserted.
My intention is to create 4 Emp objects. 2 Objects (e1 and e2) have the same hashCode. So when inserting the e1 (which is being inserted after e2), hashmap would realize that an object with the same hash value is already present (object e2). It will then compare the keys of all objects in the slot with the same hash value. If it finds an object with matching key (by calling the equals method of the Emp class below), it will replace the old value with the new one.
Please have a look at the test code below:
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
class Emp {
String name;
int age;
public Emp(String name, int age) {
this.name = name;
this.age = age;
}
public boolean equals(Object s) {
if(s instanceof Emp) {
Emp s1 = (Emp) s;
return ((s1.name.compareToIgnoreCase(this.name) == 0));
}
return false;
}
public int hashCode() {
//return (this.name.hashCode() + this.age);
return this.name.hashCode();
}
}
public class HashTest {
public static void main(String[] args) {
Emp e1 = new Emp("Terry", 26);
Emp e2 = new Emp("Terry" , 60);
Emp e3 = new Emp("John", 21);
Emp e4 = new Emp("Test", 60);
Map<Emp,Emp> emp = new HashMap<Emp, Emp>();
emp.put(e2,e2);
Emp v2 = emp.put(e1,e1);
emp.put(e3,e3);
emp.put(e4,e4);
System.out.println("Replaced Record Name: " + v2.name + " , age: " + v2.age);
for(Emp e: emp.keySet())
System.out.println("Name: " + e.name + " , age: " + e.age);
}
}
The output I was expecting: Replaced Record Name: Terry , age: 60 Name: Test , age: 60 Name: Terry , age: 26 Name: John , age: 21
The output I got: Replaced Record Name: Terry , age: 60 Name: Test , age: 60 Name: Terry , age: 60 Name: John , age: 21
I was expecting (Terry, 60) to be replaced by (Terry, 26) object. This seems to be happening as i get Replaced Record Name: Terry , age: 60 as output. However, the map contains the record Name: Terry , age: 60 instead of Name: Terry , age: 26.
Edit: Thanks everyone for the suggestion. It turns out I was making a very careless mistake. Instead of printing the values associated with a key, I was printing only the key.
As everyone has pointed out, the solution is to:
for(Emp e: emp.keySet())
{
Emp empVal = emp.get(e);
System.out.println("Name: " + empVal.name + " , age: " + empVal.age);
}