Is it possible to have an implementation for hash, where key is String
and value is function
. For the background, I have a program where there is lots of string comparison, i.e.
if(s.equalsIgnoreCase("london")
functionA();
else if(s.equalsIgnoreCase("moscow")
functionB();
else if(s.equalsIgnoreCase("delhi")
functionC();
...
and so on.
But this kind of implementation is very costly (theta(n))
, since String
comparison is done for all the if statements. If we have an hash implementation where key is String
and value is function
, we can just call something like
function = hash.Get("moscow");
function();
Its complexity is good (theta(log(1)))
.
Is it possible to do this?