equals and hashCode

Discussion in 'Java' started by Gregory A. Swarthout, Dec 19, 2003.

  1. From Object's JavDoc:

    > 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.


    It doesn't say why this must be. Does anyone know?
     
    Gregory A. Swarthout, Dec 19, 2003
    #1
    1. Advertising

  2. Gregory A. Swarthout

    Wendy S Guest

    "Gregory A. Swarthout" <> wrote in message
    news:...
    > > 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.

    >
    > It doesn't say why this must be. Does anyone know?


    Yes, but I can't explain it nearly as well as Chapter 3 of Effective Java:
    http://java.sun.com/docs/books/effective/chapters.html

    --
    Wendy
     
    Wendy S, Dec 19, 2003
    #2
    1. Advertising

  3. "Gregory A. Swarthout" <> wrote in message
    news:...
    > From Object's JavDoc:
    >
    > > 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.

    >
    > It doesn't say why this must be. Does anyone know?


    Look at what Wendy wrote. Without this requirement using hashing in general
    container classes (HashMap, HashSet) would not be feasible.
     
    Silvio Bierman, Dec 20, 2003
    #3
    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. Mike Schilling
    Replies:
    11
    Views:
    1,443
    Mike Schilling
    Jun 12, 2004
  2. Tom Dyess

    equals and hashCode

    Tom Dyess, Jun 19, 2005, in forum: Java
    Replies:
    28
    Views:
    921
    Virgil Green
    Jun 23, 2005
  3. Replies:
    6
    Views:
    475
    Alex Hunsley
    Apr 11, 2006
  4. Danno
    Replies:
    29
    Views:
    891
    Jeffrey Schwab
    Sep 16, 2006
  5. lowenbrau

    override equals and hashcode

    lowenbrau, Aug 9, 2007, in forum: Java
    Replies:
    9
    Views:
    458
    Roedy Green
    Aug 10, 2007
Loading...

Share This Page