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>

<p>This is easy in Python because methods are <a href="https://en.wikipedia.org/wiki/First-class_function" rel="nofollow">First-Class Citizens</a>.  Whereas in Java, they are not.</p>

<p>In Java 8 -however- you can create a <code>Map</code> (in this case a <code>HashMap</code>) of <code>Runnable</code></p>

<pre><code>Map&lt;Character, Runnable&gt; methods= new HashMap&lt;&gt;();
methods.put('help', () -&gt; System.out.println("Help"));
</code></pre>

<p>Then run it</p>

<pre><code>methods.get('help').run();
</code></pre>

<p>You can do it with reflection also but the code looks very much like this.</p>

java android python dictionary