When programming with C/C++ or Python I sometimes used to have a dictionary with references to functions according to the specified keys. However, I don’t really know how to have the same — or at the very least similar — behavior in Java allowing me dynamic key-function (or method, in Java slang) association.
Also, I did find the HashMap technique somebody suggested, but is that seriously the best and most elegant way? I mean, it seems like a lot to create a new class for every method I want to use.
I’d really appreciate every input on this.
You don’t need to create a full, name class for each action. You can use anonymous inner classes:
(Then store it in a static variable.)
Okay, so it’s not nearly as convenient as a lambda expression, but it’s not too bad.