2

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

2
  • It's not clear what question you're asking. What are you expecting or trying to achieve? Commented May 30, 2019 at 12:17
  • Just trying tho study how hashmap and objects hashcode behave Commented May 30, 2019 at 12:22

1 Answer 1

7

abhishek.setName("defg") is mutating a key of your HashMap (a Person instance) after you added it to the HashMap.

This causes the hashCode() of that key to change, so the get() method fails to locate it according to the new hashCode() (since it was placed in a bin that matches the original hashCode().

You are misusing the HashMap class. Keys should not be mutated after being added to the HashMap (at least properties that affect the outcome of hashCode and equals should not be mutated).

As for the behavior when not overriding equals and hashCode, in this case equals asnd hashCode do not depend on the value of name, so changing the name makes no difference. In that case, the default implementation guarantees that your key can be found in the HashMap if you are searching for the exact same Person instance you put into the Map.

Sign up to request clarification or add additional context in comments.

4 Comments

So why does not it changed till the time i override hashcode function in person object .
To add to the answer: from the documentation of the Map interface: Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map.
@abhishekvashistha I added an explanation of that.
Thanks Eran , it's lot of help , have worked on java for some time but missing some concepts , just trying to play around as much as i can . One more thing hashmap has it own hash function to get hashcodeof the object then why it does not failed in first case .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.