Your problem is likely that Key
did not implement hashCode()
and equals()
correctly (or at all). In order to be used as a HashMap
key the class has to implement these two methods to reflect "equality" of two objects.
If you create two different instances with
Key a = new Key("xyz");
Key b = new Key("xyz");
and expect them to be equal and work in a HashMap
, you have to override hashCode()
so that it returns the same value in both instances, and equals()
returns true when comparing them.
If the object identity is based on the string value, then
@Override
public int hashCode()
{
return theStringValue.hashCode();
}
and
@Override
public boolean equals(Object o)
{
return this.theStringValue.equals(o);
}
should work