What makes two keys equal in a HashMap?

A

aaronfude

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
 
I

Ingo R. Homann

Hi aaron,

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
 
T

Thomas Hawtin

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
 
M

Mike Schilling

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

George Cherry

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
 
J

Jean-Baptiste Nizet

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

Dale King

Mike said:
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.
 
M

Mike Schilling

Dale King said:
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.
 
D

Dale King

Mike said:
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.
 
M

Mike Schilling

Dale King said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top