• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

What are the consequences of not following equals and hashCode contract

 
Ranch Hand
Posts: 391
1
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So what if we don`t follow equals and hashCode contract that hashCode must return true if equals return true for two objects.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you do not implement equals and hashCode correctly various "Collections" classes will fail to work as expected.

Bill
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Especially hash-based collections such as HashSet and HashMap.

For example: you put an object with incorrect hashSet() and equals() methods in a HashSet, and then when you check if it's in the HashSet, it will tell you it isn't there, even though you just put it in.

What happens exactly depends on the details of how collections such as HashSet and HashMap are implemented, and it's in general not easily possible to predict what will happen. The only thing that's important to remember if that these collections might not do what you expect if you put in objects that have incorrectly implemented hashSet() and equals() methods.
 
Marshal
Posts: 80634
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's have a class Foo which correctly overrides equals and not hashCodeThis is unpredictable; there is a 1 in 16 chance that you will get true-true-1!
 
Mahtab Alam
Ranch Hand
Posts: 391
1
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Set does not allow duplicates , now how will set find out whether two weird objects are same or not.
The output of above program just shows two objects .
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All those instances of Weird are equal (according to the equals method).
However, according to hashCode they are not (based on the length of their status).
Consequently where you should have only the one entry in the HashSet you now have two.
 
Campbell Ritchie
Marshal
Posts: 80634
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mahtab Alam wrote:...

Don't use if (something) return somethngElse; else return somethingDifferent;

In the case of a boolean simply write
return o instanceof Weird && ...;

It's all in the old code conventions. I wish they would update those conventions rather than label them as out of date.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic