I have been having some trouble using the function containsKey. I wrote a small program to show where I am expecting containsKey to give me a different result:
HashMap<IdentifierInterface, Set<NaturalNumberInterface>> hashMap;
HashMap<StringBuffer, Integer> works;
TryHashmap(){
hashMap = new HashMap<IdentifierInterface, Set<NaturalNumberInterface>>();
works = new HashMap<StringBuffer, Integer>();
}
private void start() {
Identifier iden = new Identifier('a');
NaturalNumber nn = new NaturalNumber('8');
Set<NaturalNumberInterface> set = new Set<NaturalNumberInterface>();
set.insert(nn);
hashMap.put(iden, set);
System.out.println(hashMap.containsKey(iden));
Identifier newIden = new Identifier('a');
System.out.println(hashMap.containsKey(newIden)); //TODO why is this not true?
iden.init('g');
System.out.println(hashMap.containsKey(iden));
}
public static void main(String[] argv) {
new TryHashmap().start();
}
The constructor of the Identifier class is as follows, the init() is similar but it will remove anything that was in the identifier before.
Identifier(char c){
iden = new StringBuffer();
iden.append(c);
}
I put something into the hashmap using an Identifier as key, but when I try to use an Identifier with a different name but with the same content the containsKey function returns false where I am expecting a true. (the output prints true false true)
Thanks in advance!