Re: hashCode

Discussion in 'Java' started by Jan Burse, Aug 13, 2012.

  1. Jan Burse

    Jan Burse Guest

    To: Jan Burse
    From: "Jan Burse" <jan.burse@1:261/38.remove-m2z-this>

    To: Jan Burse
    From: Jan Burse <>

    Jan Burse schrieb:
    > Maybe it would make sense to spell out what the contract
    > for hashCode() is.


    Those who are interested can read the original text. It is found here:

    http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode()

    ---------------------------------------------------------------

    "If two objects are equal according to the equals(Object)
    method, then calling the hashCode method on each of the
    two objects must produce the same integer result."

    That is:

    /* invariant that should hold */
    if a.equals(b) then a.hashCode()==b.hashCode()

    ----------------------------------------------------------------

    "It is not required that if two objects are unequal according
    to the equals(java.lang.Object) method, then calling the
    hashCode method on each of the two objects must produce
    distinct integer results. However, the programmer should be
    aware that producing distinct integer results for unequal
    objects may improve the performance of hash tables."

    That is:

    /* not implied and thus not required by the invariant */
    if a.hashCode()==b.hashCode() then a.equals(b)

    It is also quite unlikely that a hashCode() would satisfy the later, although
    the closer it comes to the later, the better it works for HashMap, etc..

    ----------------------------------------------------------------

    There is no reference to Comparable and the compareTo. If I am not using
    HashMap or HashSet in my application, and still override equals, I do not need
    to implement hashCode(). I could for example use TreeMap or TreeSet and
    implement the Comparable interface. There is a reference to equals() back from
    Comparable though.

    http://docs.oracle.com/javase/6/docs/api/java/lang/Comparable.html

    Bye

    -+- BBBS/Li6 v4.10 Dada-1
    + Origin: Prism bbs (1:261/38)
    -+- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
    --- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24
    Jan Burse, Aug 13, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green

    hashCode for byte[]

    Roedy Green, Aug 22, 2003, in forum: Java
    Replies:
    1
    Views:
    410
    Dale King
    Aug 22, 2003
  2. Marco
    Replies:
    10
    Views:
    749
  3. Gregory A. Swarthout

    equals and hashCode

    Gregory A. Swarthout, Dec 19, 2003, in forum: Java
    Replies:
    2
    Views:
    332
    Silvio Bierman
    Dec 20, 2003
  4. kelvSYC

    Designing hashCode() methods

    kelvSYC, Dec 23, 2003, in forum: Java
    Replies:
    1
    Views:
    350
    Ulrich Stern
    Dec 24, 2003
  5. Dimitri Pissarenko

    Hashcode of primitive types

    Dimitri Pissarenko, Jan 29, 2004, in forum: Java
    Replies:
    5
    Views:
    5,792
    Hylander
    Jan 29, 2004
Loading...

Share This Page