I have a hasmap with a key object,
HashMap<Key, Object> test;
and make new Key(“the same”) as key..
so its like..:
test.put(new Key("the same"), someObject);
(without storing that key in a variable)
so.. after a while… i want to access the hashmap,
because i do not have the object, i’ve tried to make new Key(“the same”) and make it as a key.
But it didnt work.
How to make it work? (without saving the first object “key” in a variable)
So meanwhile, for now, im using String object as a key.
HashMap<String, Object>
You need to implement
hashCodeandequalsonKey. The default implementation of these methods simply checks for instance equality (in other words, twoObjects will only be equal if they are in fact the same object).Further reading
Effective Java – Methods common to all objects