File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap key problem

 
Phil Lesh
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.. 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
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter,
This is the exact equality that I was looking for, thank you very much for your help! I worked great!
Phil
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic