I want to store methods pointer in map to execute them based on a string value. Аrom what I found, I can use Map<String, Runnable>
to do it, but the problem is I want to get the return value from the method.
Say I have something like this:
private Map<String, Runnable> timeUnitsMap = new HashMap<String, Runnable>() {{
timeUnitsMap.put("minutes", () -> config.getMinutesValues());
}}
The method config.getMinutesValues()
is from another class.
How can I do int value = timeUnitsMap.get("minutes").run();
or to store something else in the map (instead of Runnable
) in order to get the value from the function in the map?