aspose file tools*
The moose likes Java in General and the fly likes HashMap key problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashMap key problem" Watch "HashMap key problem" New topic
Author

HashMap key problem

Phil Lesh
Greenhorn

Joined: Oct 16, 2002
Posts: 17
Hi!
A program I am working on now uses HashMaps. The key is a NodePair structure shown below. When I put things into the HashMap, I can see that the information has been entered (using JBuilder's watch tool) but when I extract information using the command:
(<cast to object> myMap.get(new NodePair(start, end)) where start & end are Strings
I simply get a null value. Maybe the problem has something to do with the fact that if the equals method was used to check the information in the HashMap and the new NodePair, they would be different. Can anyone shed some light on this? Thank you in advance!
Phil
------------- code ------------------
public class NodePair
{
private String startNodeName;
private String endNodeName;
/**
* Class constructor setting the start & end node names.
*/
public NodePair(String startNodeName, String endNodeName)
{
this.startNodeName = startNodeName;
this.endNodeName = endNodeName;
}
/**
* Calculates the combined hash code of the node pair
*/
public int hashCode()
{
// multiply by 3 so that you can distinguish between
// directed and undirected edge
int hashKey = (3 * startNodeName.hashCode()) ^ (endNodeName.hashCode());
return hashKey;
}
public String getStartNodeName()
{
return startNodeName;
}
public String getEndNodeName()
{
return endNodeName;
}
}
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Phil Lesh:
Maybe the problem has something to do with the fact that if the equals method was used to check the information in the HashMap and the new NodePair, they would be different.

Have you tried this?
What did you find?
Dave
Phil Lesh
Greenhorn

Joined: Oct 16, 2002
Posts: 17
For some reason the hash code generated by "new NodePair(start, end)" is slightly different to the one present in the HashMap but since the values stored in the HashMap contain start & end strings they must have also been placed there using the key above - I have checked and they are placed in the HashMap using "NodePair nodePairKey = new NodePair(startNodeName, endNodeName);" key.
Thanks
Phil
Phil Lesh
Greenhorn

Joined: Oct 16, 2002
Posts: 17
.. i also tried making the hashCode the same for both the lookup and the insertion and still i get null from "myMap.get(new NodePair(start, end))".
Phil
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
You need to override both hashCode() and equals() for HashMap to work. They need to be consistent, i.e. if a.equals(b) then a.hashCode() == b.hashCode() (but not necessarily the other way around).
- Peter
Phil Lesh
Greenhorn

Joined: Oct 16, 2002
Posts: 17
Peter den Haan or anyone else, can you tell me how the equals() method would look like becuase I tried this and it didn't work:
public boolean equals(NodePair np)
{
if(this.hashCode() == np.hashCode())
{
return true;
}
else
{
return false;
}
}
AND THIS:
public boolean equals(NodePair np)
{
return (this == np);
}
I also realised that when I put print statements in this function they are not printed, meaning that the method doesn't get invoked anyway. Thanks in advance!
Phil
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
The first problem with this is that identical hash codes don't necessarily mean they're equal. (It should be true the other way around -- when they're equal, their hash codes must be identical). The second is that this equals() method will never be used by the HashMap because it has the wrong signature (it takes a NodePair rather than an Object).
This doesn't have the right signature, and is otherwise the default implementation in java.lang.Object. But is object identity what you need? Looking at your hashCode, I would expect an equals() that looks likeIt tests for equality of the start and end node names. But only you can say whether this is exactly the equality you need.
- Peter
Phil Lesh
Greenhorn

Joined: Oct 16, 2002
Posts: 17
Peter,
This is the exact equality that I was looking for, thank you very much for your help! I worked great!
Phil
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap key problem