Trying to check how hashmap works
public class hashmapcheck {
public static void main(String args[]) {
Person abhishek = new Person("abhishek");
Map<Person,String> mapCheck = new HashMap<Person,String>();
mapCheck.put(abhishek,"ancd");
abhishek.setName("defg");
System.out.println(mapCheck.get(abhishek)); //line which i try to undertand
}
}
public class Person {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person(String name) {
this.name = name;
}
String name;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return name != null ? name.equals(person.name) : person.name == null;
}
@Override
public int hashCode() {
return name != null ? name.hashCode() : 0;
}
}
if equals and hashcode is not overriden for person class it print ancd, but when i override it it will print null. what i thought when i store objectin hashmap it will store reference of that hashmap what is going wrong