When I call containsKey(value) it returns false when the key is in the map. I would appreciate it if someone could check my code!
I have already tried printing out they key and hashmap toString method and they Key is in the map.
HashMap<IdentifierInterface, T> hm = new HashMap<IdentifierInterface, T>();
public T getMemory(String v) {
if(hm.containsKey(v)){
return hm.get(v);
}
return null;
}
Hashcode and Equals methods in IdentifierInterface
:
public int hashCode() {
return identifier.toString().hashCode();
}
public boolean equals(Object toCompare) {
if (toCompare instanceof Identifier) {
if (toCompare.hashCode() == this.hashCode()) {
return true;
}
}
return false;
}
The expected result is true and actual is false in getMemory().
The HashMap
keys are of type IdentifierInterface
, but you're calling containsKey(String)
.
In your equals()
you however use instanceof Identifier
which will always return false
when a String
is passed in.
So turn your String
into an IdentifierInterface
(or make the keys Strings
instead).