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

HashCode

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When an object is created in a java class, does it carry system generated hashcode ? If so, then every object will carry unique hashcode, right ?


In this example, a1 and a2 will have unique hashodes for themselves, which will be different from each other. So if I compare aiHashcode and a2Hashcode, it should return false. So how can I make sure that a1Hashcode and a2Hashcode have same values , since they are system generated values ?
If I override hashCode() to return same hashCode for two objects, then it will return same hashCode even when object are not equal.

Please explain in some detail, rather than pointing to some link. I have done reading and then I am posting here.

Thanks
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So how can I make sure that a1Hashcode and a2Hashcode have same values ?

override the hashCode() method in your A.java file and return same value.Although not recommended.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nirjari patel wrote:
In this example, a1 and a2 will have unique hashodes for themselves, which will be different from each other. So if I compare aiHashcode and a2Hashcode, it should return false. So how can I make sure that a1Hashcode and a2Hashcode have same values , since they are system generated values ?
If I override hashCode() to return same hashCode for two objects, then it will return same hashCode even when object are not equal.


There are few rules which one has to know regarding overriding hashCode(). The few which I could recall are-
  • If- instance1.equals(instance2) is TRUE then instance1.hashCode() == instance2.hashCode() should be TRUE.
  • If- instance1.equals(instance2) is FALSE then instance1.hashCode() == instance2.hashCode() can be TRUE or FALSE but ideally it should be FALSE as it improves the performance of the hashtables.
  • Repeated calls for the hashCode on the same instance should return the same value each time provided the state of the instance(those fields used in the equals() method) doesn't change

  • And while calculating the hashCode its better to use the same fields as used in the equals().

    nirjari patel wrote:
    Please explain in some detail, rather than pointing to some link. I have done reading and then I am posting here.

    Thanks



    From what sources/books have you referred to understand this?
    Update: You might want to check this and this to further clear your doubts.
     
    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

    nirjari patel wrote:If so, then every object will carry unique hashcode, right ?


    No, you should never assume that every object has a unique hash code. A hash code is not a unique identifier of an object.

    It's easy to see why that's not possible. A hash code is a 32-bit integer. That means that there are 2^32 = 4,294,967,296 possible hash codes.

    Now, let's look at strings of exactly 10 letters (A-Z). There are 26^10 = 141,167,095,653,376 possible combinations of 10 letters. That's much more than the number of possible hash codes. So there must be lots of different 10-letter strings that have the same hash code.

    The most important thing to remember is that when two objects are equal (the equals() method returns true for those two objects), then the hash code of those objects must be the same.

    Note that this works only in one direction: the converse (hashcodes are equal => objects are equal) does not hold. It is allowed for two objects to be not equal, but have the same hash code.
     
    Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic