Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java equivalent of function mapping in Python

In python, if I have a few functions that I would like to call based on an input, i can do this:

lookup = {'function1':function1, 'function2':function2, 'function3':function3}
lookup[input]()

That is I have a dictionary of function name mapped to the function, and call the function by a dictionary lookup.

How to do this in java?

like image 524
Ross Avatar asked Jun 01 '09 11:06

Ross


People also ask

Is there a map function in Python?

Python's map() is a built-in function that allows you to process and transform all the items in an iterable without using an explicit for loop, a technique commonly known as mapping. map() is useful when you need to apply a transformation function to each item in an iterable and transform them into a new iterable.

Is there a map function in Java?

The map() function is a method in the Stream class that represents a functional programming concept. In simple words, the map() is used to transform one object into other by applying a function. That's why the Stream. map(Function mapper) takes a function as an argument.

What is the equivalent of map in Python?

Map in Python is a function that works as an iterator to return a result after applying a function to every item of an iterable (tuple, lists, etc.). It is used when you want to apply a single transformation function to all the iterable elements. The iterable and function are passed as arguments to the map in Python.

Is Java HashMap the same as Python dictionary?

Python's dict class is an implementation of what the Python documentation informally calls "mapping types". Internally, dict is implemented using a hashtable. Java's HashMap class is an implementation of the Map interface. Internally, HashMap is implemented using a hashtable.


3 Answers

There are several ways to approach this problem. Most of these were posted already:

  • Commands - Keep a bunch of objects that have an execute() or invoke() method in a map; lookup the command by name, then invoke the method.
  • Polymorphism - More generally than commands, you can invoke methods on any related set of objects.
  • Finally there is Reflection - You can use reflection to get references to java.lang.Method objects. For a set of known classes/methods, this works fairly well and there isn't too much overhead once you load the Method objects. You could use this to, for example, allow a user to type java code into a command line, which you execute in real time.

Personally I would use the Command approach. Commands combine well with Template Methods, allowing you to enforce certain patterns on all your command objects. Example:

public abstract class Command {
  public final Object execute(Map<String, Object> args) {
    // do permission checking here or transaction management
    Object retval = doExecute(args);
    // do logging, cleanup, caching, etc here
    return retval;
  }
  // subclasses override this to do the real work
  protected abstract Object doExecute(Map<String, Object> args);
}

I would resort to reflection only when you need to use this kind of mapping for classes whose design you don't control, and for which it's not practical to make commands. For example, you couldn't expose the Java API in a command-shell by making commands for each method.

like image 62
Mr. Shiny and New 安宇 Avatar answered Sep 19 '22 15:09

Mr. Shiny and New 安宇


Java doesn't have first-class methods, so the command pattern is your friend...

disclamer: code not tested!

public interface Command 
{
    void invoke();
}

Map<String, Command> commands = new HashMap<String, Command>();
commands.put("function1", new Command() 
{
    public void invoke() { System.out.println("hello world"); }
});

commands.get("function1").invoke();
like image 30
Dan Vinton Avatar answered Sep 17 '22 15:09

Dan Vinton


Unfortunately, Java does not have first-class functions, but consider the following interface:

public interface F<A, B> {
  public B f(A a);
}

This models the type for functions from type A to type B, as first-class values that you can pass around. What you want is a Map<String, F<A, B>>.

Functional Java is a fairly complete library centered around first-class functions.

like image 35
Apocalisp Avatar answered Sep 21 '22 15:09

Apocalisp