• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Equals e HashCode

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !

I have a doubt in this code.

The source is Iquisition.



The result is true.

But the class should override the hashCode() method to result true??
I think that hashCode() of the class Object provides different numbers of hashCode to each object .

Thanks!
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well as far as i see it, you are only checking for equals here and the override is valid and you are calling the valid overridden method and within it the operations are checking for the values of the instance variable. So the equals will return true for meaningful equivalents.But yes this will have different hashcodes. Try to check that by calling the hashcode method on the reference variable. Well the contract says that you must override it but if you don't there is no harm but it will result in a bad design that's all.
 
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
There is an equals() method which compares two Teste objects by checking if their member variable x contains the same value. Then you call equals() to compare two Teste objects which both have x = 3. Ofcourse the result will be true.

You are simply calling the equals() method, which works just like any other method. There is no magic involved with hash codes here.

Hash codes are used by certain collection classes, such as HashSet and HashMap. If you would want to put Teste objects in a HashSet, or use Teste objects as keys in a HashMap, then class Teste would need to implement the hashCode() method so that two Teste objects which are equal have the same hash code. If class Teste does not have a correct hashCode() method, then you will get strange results when putting Teste objects in a hash code based collection.
 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two objects are equal but it's giving different Hascodes. Can you explain me in how can implent hashcode in below example so that i can use them in hashMap for searching.



 
Jesper de Jong
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
To make instances of a class useable as keys in a HashMap, you must make sure that they obey the hash code contract:

  • When two objects are equal, their hash code value must be the same
  • When two objects are not equal, it doesn't matter (their hash code values might be the same, or different)

  • You must implement your hashCode() and equals() in such a way that this holds.
     
    Doody calls. I would really rather that it didn't. Comfort me wise and sterile tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic