As @bbsmooth is saying, keys are cast to strings so that cannot work.
Secondly, if you could use an object as a key, how would it work? An object is not the same as the values of the properties stored in the object, this “key”:
{"password" : 123}
Will not be the same as this “key”:
{"password" : 123}
So if you could do it, then trying to look up the value, like
Dictionary[{"password" : 123}]
Just won’t work because that key doesn’t match.
You can use objects as keys in Maps, but you still have exactly the same issue: it’s going to prevent you looking up by key.
What you are asking for is solved by something called a hash map, which is a map of hashed keys => values. JS objects are basically hash maps for example.
In this case, you have to provide some kind of hashing function that changes
{ "password": 123 }
Into (eg) a string when you store it. ie it gets hashed.
So for example on the object you want to use as a key,
- that could be implemented as a class
- that class could implement
hash
(to convert the object) and equals
(to be able to look up & compare the object as a key) methods.
Then you could subclass Map
, so that when you call set
or whatever, it first checks if it can hash the object, then does that and sets the values. Then same thing for the other methods you require: looking up values, checking if a given “key” is in the map, etc.