What makes two keys equal in a HashMap?

Discussion in 'Java' started by aaronfude@gmail.com, Aug 3, 2005.

  1. Guest

    Hi,

    I have always assumed that HashMap.get(obj) looks for the key that
    key.compareTo(obj) == 0. But apparently, that is not the case:

    public static class GoodAsKeys implements Comparable {
    private int myInt;

    public GoodAsKeys(int inInt) { myInt = inInt; }

    public int compareTo(Object inObject) {
    return myInt - ((GoodAsKeys) inObject).myInt;
    }
    }

    public static void main(String[] args) {
    GoodAsKeys a = new GoodAsKeys(5);
    GoodAsKeys b = new GoodAsKeys(5);

    HashMap map = new HashMap();
    map.put(a, "World");
    System.out.println(map.get(b));
    }

    This program prints out "null". What have I missed here?

    Many thanks in advance!

    Aaron
     
    , Aug 3, 2005
    #1
    1. Advertising

  2. Hi aaron,

    wrote:
    > Hi,
    > ...
    > This program prints out "null". What have I missed here?


    Reading the doku and recognizing that you must overwrite equals() and
    hashCode()!

    Ciao,
    Ingo
     
    Ingo R. Homann, Aug 3, 2005
    #2
    1. Advertising

  3. wrote:
    >
    > I have always assumed that HashMap.get(obj) looks for the key that
    > key.compareTo(obj) == 0. But apparently, that is not the case:


    Wouldn't be much of a *hash* map then, would it?

    TreeMap uses Comparable/Comparator.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Aug 3, 2005
    #3
  4. <> wrote in message
    news:...
    > Hi,
    >
    > I have always assumed that HashMap.get(obj) looks for the key that
    > key.compareTo(obj) == 0. But apparently, that is not the case:


    No, it looks for the key such that key.equals(obj) is true.
     
    Mike Schilling, Aug 3, 2005
    #4
  5. <> wrote in message
    news:...
    > Hi,
    >
    > I have always assumed that HashMap.get(obj) looks for the key that
    > key.compareTo(obj) == 0.


    You don't understand the basic idea of a hashed collection.
    I suggest you grab an introductory computer science book
    and read about hash functions, hash buckets, and hash tables.
    Then you'll understand why compareTo() is not germane, but
    hashCode() and equal() are. You need compareTo() for a
    TreeMap, but not for a HashMap. You need to override
    hashCode() and equal() for a HashMap but not for a TreeMap.

    George W. Cherry
     
    George Cherry, Aug 4, 2005
    #5
  6. >You need to override
    >hashCode() and equal() for a HashMap but not for a TreeMap


    In fact, if you want to obey the general contract for the Map
    interface, you have to implement equals in a way that is consistent
    with compareTo. And if you want to obey the general equals/hashCode
    contract, you have to implement hashCode so that two equal objects have
    the same hashCode.
    A TreeMap will not use the equals and hashCode methods, and will work
    if they are not defined or defined inconsistently, but it's a good idea
    to have them implemented right.

    JB.
     
    Jean-Baptiste Nizet, Aug 4, 2005
    #6
  7. Dale King Guest

    Mike Schilling wrote:
    > <> wrote in message
    > news:...
    >
    >>Hi,
    >>
    >>I have always assumed that HashMap.get(obj) looks for the key that
    >>key.compareTo(obj) == 0. But apparently, that is not the case:

    >
    >
    > No, it looks for the key such that key.equals(obj) is true.


    To be entirely precise it's actually:

    key==null ? obj==null : key.equals(obj)

    It also requires that if the key is non-null that:

    key.hashCode() == obj.hashCode()

    but that is required by the contract of equals anyway.

    --
    Dale King
     
    Dale King, Aug 15, 2005
    #7
  8. "Dale King" <> wrote in message
    news:eHRLe.268655$xm3.215581@attbi_s21...
    > Mike Schilling wrote:
    >> <> wrote in message
    >> news:...
    >>
    >>>Hi,
    >>>
    >>>I have always assumed that HashMap.get(obj) looks for the key that
    >>>key.compareTo(obj) == 0. But apparently, that is not the case:

    >>
    >>
    >> No, it looks for the key such that key.equals(obj) is true.

    >
    > To be entirely precise it's actually:
    >
    > key==null ? obj==null : key.equals(obj)


    Right you are. This sort of thing is needed so often, there should really
    be a method on Object

    public static boolean Object equals(Object a, Object b)
    {
    return a == null ? b == null : a.equals(b);
    }

    ..NET has one.
     
    Mike Schilling, Aug 15, 2005
    #8
  9. Dale King Guest

    Mike Schilling wrote:
    > "Dale King" <> wrote in message
    > news:eHRLe.268655$xm3.215581@attbi_s21...
    >>>
    >>>No, it looks for the key such that key.equals(obj) is true.

    >>
    >>To be entirely precise it's actually:
    >>
    >> key==null ? obj==null : key.equals(obj)

    >
    >
    > Right you are. This sort of thing is needed so often, there should really
    > be a method on Object
    >
    > public static boolean Object equals(Object a, Object b)
    > {
    > return a == null ? b == null : a.equals(b);
    > }
    >
    > ..NET has one.


    The way HashMap does it is that if you pass null it actually replaces it
    with a singleton object called NULL_OBJECT. It therefore gets a real
    equals and hashCode method that can be called.

    --
    Dale King
     
    Dale King, Aug 17, 2005
    #9
  10. "Dale King" <> wrote in message
    news:yoGMe.259351$_o.61314@attbi_s71...
    > Mike Schilling wrote:
    >> "Dale King" <> wrote in message
    >> news:eHRLe.268655$xm3.215581@attbi_s21...
    >>>>
    >>>>No, it looks for the key such that key.equals(obj) is true.
    >>>
    >>>To be entirely precise it's actually:
    >>>
    >>> key==null ? obj==null : key.equals(obj)

    >>
    >>
    >> Right you are. This sort of thing is needed so often, there should
    >> really be a method on Object
    >>
    >> public static boolean Object equals(Object a, Object b)
    >> {
    >> return a == null ? b == null : a.equals(b);
    >> }
    >>
    >> ..NET has one.

    >
    > The way HashMap does it is that if you pass null it actually replaces it
    > with a singleton object called NULL_OBJECT. It therefore gets a real
    > equals and hashCode method that can be called.


    Sure, but that's not always feasible.
     
    Mike Schilling, Aug 17, 2005
    #10
    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. Vince Darley
    Replies:
    4
    Views:
    4,573
    emilchacko
    Mar 2, 2010
  2. Replies:
    14
    Views:
    14,244
    Zer0Frequency
    Feb 16, 2005
  3. Charles B. Summers
    Replies:
    6
    Views:
    777
    kayodeok
    Jun 28, 2004
  4. William Payne
    Replies:
    4
    Views:
    476
    Patrick Kowalzick
    Sep 9, 2003
  5. Rakesh
    Replies:
    10
    Views:
    12,274
    Mike Schilling
    Apr 8, 2008
Loading...

Share This Page