aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please Explain (Hashcode) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please Explain (Hashcode)" Watch "Please Explain (Hashcode)" New topic
Author

Please Explain (Hashcode)

Latha Kota
Ranch Hand

Joined: Mar 13, 2003
Posts: 35
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 ]
Richard Quist
Ranch Hand

Joined: Feb 18, 2004
Posts: 96
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


    Rich
    SCJP 1.4
    Latha Kota
    Ranch Hand

    Joined: Mar 13, 2003
    Posts: 35
    Thanks Rich, but can you also give me an example of what you have explained.
    Latha
    Richard Quist
    Ranch Hand

    Joined: Feb 18, 2004
    Posts: 96
    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 ]
    Barry Gaunt
    Ranch Hand

    Joined: Aug 03, 2002
    Posts: 7729
    Here's the "classic" of equals/hashcode info:
    Manish's Epic
    [ April 10, 2004: Message edited by: Barry Gaunt ]

    Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
    Getting someone to think and try something out is much more useful than just telling them the answer.
    Maulin Vasavada
    Ranch Hand

    Joined: Nov 04, 2001
    Posts: 1873
    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

    Joined: Mar 13, 2003
    Posts: 35
    Thank you very much Rich, I have a clearer idea of hashcode and equals method now.
    Latha
    sameer kumar
    Greenhorn

    Joined: Mar 22, 2004
    Posts: 13
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Please Explain (Hashcode)