Is there a way to define function-dictionaries in android/Java like in python?
<p>In python it is possible to create a dictionary of functions (<code>foo,bar</code>) and call them by accessing the dictionaries element:</p>

<pre><code>d = {'a':foo, 'b':bar}
d[val]()
</code></pre>

<p>In this example if <code>var='a'</code> the function <code>foo</code> is called, and if <code>var='b'</code> the function <code>bar</code> is called (other choices lead to errors).</p>

<p>Can I do something like that in android/Java?</p>

<pre><code>import java.util.*;

class HashMethod {
    public void foo() {
        System.out.println("foo");
    }

    public void bar() {
        System.out.println("bar");
    }

    public static void main(String[] args) throws IllegalAccessException {
        HashMethod obj = new HashMethod();
        Map&lt;Character, java.lang.reflect.Method&gt; methods = new HashMap&lt;Character, java.lang.reflect.Method&gt;();
        try {
            Character val = 'a';
            java.lang.reflect.Method method = obj.getClass().getMethod("foo");
            methods.put(val, method);
            Character val2 = 'b';
            java.lang.reflect.Method method2 = obj.getClass().getMethod("bar");
            methods.put(val2, method2);
        } catch (SecurityException e) {
            System.out.print("exc1" + e.toString());
        } catch (NoSuchMethodException e) {
            System.out.print("exc2" + e.toString());
        }
        Scanner s = new Scanner(System.in);
        Character val = s.next().charAt(0);
        try {
            java.lang.reflect.Method methodToRun = ((java.lang.reflect.Method) methods.get(val));
            methodToRun.invoke(obj, null);
        } catch (Exception e) {
            System.out.print("invalid input");
        }
    }
}
</code></pre>

java android python dictionary