• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Please Explain (Hashcode)

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody explain me what is meant by correct and incorrect implementation of hashcode(). Thanks in advance.
Latha

Modified title to be a little more specific.
[ April 10, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Latha Kota:
Can anybody explain me what is meant by correct and incorrect implementation of hashcode(). Thanks in advance.
Latha


Correct means that not only does the method compile but it also follows these rules:
  • if two objects are considered equal (by a call to the equals() method), they must have the same hashCode
  • if two items have the same hashCode they may be equal (note "may", NOT "must")
  • calling hashCode() multiple times on the same object during the same instance of the running program must return the same value so long as the object's state that is used in the equals() implementation hasn't changed (no random factor such as the current time should be used in the hashCode calculation)

  • Hope that helps
     
    Latha Kota
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Rich, but can you also give me an example of what you have explained.
    Latha
     
    Richard Quist
    Ranch Hand
    Posts: 96
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'll try... take a look at this

    Running this gives the following:
    t1 hashcode: 3
    t1.equals(t1)? true
    t1 hashCode: 3 t2.hashCode: 3 are equal?: true
    t1.equals(t2)? true
    t1 hashCode: 3 t3.hashCode: 3 are equal?: true
    t1.equals(t3)? false
    Things to note:
  • t1 equals itself (t1.equals(t1))
  • t1.equals(t2) is true AND their hashcodes are the same
  • t1.equals(t3) is FALSE even though their hashcodes are the same


  • You might also want to look at the equals() and hashCode() documentation in the Object class
    Hope this helps
    [ April 09, 2004: Message edited by: Richard Quist ]
     
    Ranch Hand
    Posts: 7729
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here's the "classic" of equals/hashcode info:
    Manish's Epic
    [ April 10, 2004: Message edited by: Barry Gaunt ]
     
    Ranch Hand
    Posts: 1873
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Alright
    Now, Latha if you look at my Java RAQ there is a question which asks "why we need to have a contract that is - if two objects are equal by equals() then its necessary to have hashCodes same"...
    Its simple logic though but food for thought
    Regards
    Maulin
     
    Latha Kota
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you very much Rich, I have a clearer idea of hashcode and equals method now.
    Latha
     
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    lata kota is u r native place
    i think u r an indian kota (Rajasthan)
    try to see the JAVA DOCS API REGADING TO HASH CODE U WILL FIND VERY GOOD
    EXPLATIONATION THERE.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!