-4

I encountered this question during OCPJP.

Given the code:

public class Person {
    private String name;
    public Person(String name) {
        this.name = name;
    }
    public boolean equals(Object o) {
        if (!(o instanceof Person)) return false;
        Person p = (Person)o;
        return p.name.equals(this.name);
    }
}

And the following statements (only one of which is true):

  • A. Compilation fails because the hashCode method is not overridden
  • B. A HashSet could contain multiple Person objects with the same name.
  • C. All Person objects will have the same hash code because the hashCode method is not overridden
  • D. If a HashSet contains more than one Person object with name="Fred" then removing another Person, also with name="Fred", will remove them all.

The answer is (B), but I don't understand why. Can somebody please explain this?

Krease
  • 15,805
  • 8
  • 54
  • 86
  • Possible duplicate of [How does a Java HashMap handle different objects with the same hash code?](http://stackoverflow.com/questions/6493605/how-does-a-java-hashmap-handle-different-objects-with-the-same-hash-code) – Savior Apr 07 '16 at 15:40
  • Do you know why classes overriding `equals` should also override `hashCode`? – Andy Turner Apr 07 '16 at 15:44
  • 2
    Don't post a screen shot of text. Post text as text. – khelwood Apr 07 '16 at 15:44
  • Yes I know that, but the problem is the given answer for this question. How does this hashset could contain multiple Person objects with the same name ?? – Sidath Bhanuka Randeniya Apr 07 '16 at 16:02

1 Answers1

1

As a general rule: always make sure that if obj1.equals(obj2), then obj1.hashCode() == obj2.hashCode()

In your case, since hashCode isn't being overridden, it's using the default one defined in Object, which does not take into account any properties of the object - you'll generally find that new Object().hashCode() != new Object().hashCode().

With this case, if you have two Person objects with the same name, they will most likely end up with different hashCode values, allowing them to coexist in the set: your HashSet can contain multiple Person objects with the same name, which is answer (B).

This is why it's important to override both hashCode and equals - if your objects are equal, but don't generate the same hashCode, they won't behave properly when using them in hash-based collections. This related question has a bit more details on what happens in different cases.

Community
  • 1
  • 1
Krease
  • 15,805
  • 8
  • 54
  • 86
  • But I have heard that actually equals method is responsible for identifying unique items in a set. Which it does in the above equals method according to my knowledge. Is it ?? Then how two similar objects can co-exist in the set ?? – Sidath Bhanuka Randeniya Apr 07 '16 at 19:41
  • 1
    Ok, took a little time to understand. When hashCode() direct the object to a certain bucket then it checks for all the element in it for equality. if no equal item found it is added to it. I hope my understand is correct !! – Sidath Bhanuka Randeniya Apr 07 '16 at 19:49
  • Yup, you've got it :) – Krease Apr 07 '16 at 20:23