3

In Java 8, I have a HashSet h (correctly typed by generics), with hashCode/equals methods implemented up to the bottom of the type hierarchy - apparently correctly as eclipse did it for me.

In some (rare) cases, the method invokation h.contains(e) returns false - which is wrong (to my understanding) as there is an element e' in the set with both equal hashCodes and the equals method e.equals(e') returning true.

To make my confusion even greater, new HashSet<>(h).contains(e) returns the correct answer true.

How can this EVER be?

As this rare case is generated at some point in a big program which is hardly retraceable, I cannot provide a MWE, but I can give more information if desired.

Jens
  • 67,715
  • 15
  • 98
  • 113
lukas.coenig
  • 541
  • 1
  • 7
  • 19
  • Related, possibly duplicate: http://stackoverflow.com/questions/4718009 – blgt Jan 27 '15 at 12:04
  • I wouldn't consider this a duplicate as my question is more specific. If you already know what you're looking for, I agree that the related post answers my question, too. But before asking, I tried my best to find an answer and failed. So maybe people looking for "strange behavior of contains method" or something similar might find this useful. – lukas.coenig Jan 27 '15 at 12:26

1 Answers1

5

What you describe can happen if you add an element to the HashSet, change some property of that element which is used in the calculation of hashCode() (thus changing the hashCode() of that element) and then call contains(e), which searches for the element based on the new value of hashCode(), but can't find it, since it was stored in a location the matches the old value of hashCode(). When you put the element in a new HashSet, it is stored in a location matching the new hashCode, so contains works.

Eran
  • 387,369
  • 54
  • 702
  • 768