I have a set of string which I will be using as the Keys and for a particular string I want a function to be called. So is it possible to assign a function to the value in the pair?
exampleMap.get("SOME_STRING"); // should call a function abc();
I have a set of string which I will be using as the Keys and for a particular string I want a function to be called. So is it possible to assign a function to the value in the pair?
exampleMap.get("SOME_STRING"); // should call a function abc();
Encapsulate your function in a Java interface:
public interface Task {
void doSomething();
}
and then pouplate the map with instances of this interface:
map.put("someString", new Task() {
@Override
public void doSomething() {
System.out.println("foo");
}
});
map.put("someOtherString", new Task() {
@Override
public void doSomething() {
System.out.println("bar");
}
});
Then to call the function (task) associated with a given string s
:
map.get(s).doSomething();
No, java does not support any method objects yet. You could use reflection or try to replace your strings with enum constants and call the method on them.
Java doesn't support it directly, but you can do it indirectly using interfaces.
public interface Funct {
public void apply();
}
and then when you want to use it as value, do something like..
Map<String, Funct> aMap = new HashMap<String, Funct>();
aMap.put("foo", new Funct() { public void apply() { System.out.println("bar"); } });
If you want this to be transparent to the user of the Map, you have to implement your own Map.
Yes. Although, it's the java version of a "function": an anonymous class.
Best illustrated with an example:
interface MyFunction {
public int calculate(int a, int b);
}
Map<String, MyFunction> map = new HashMap<String, MyFunction>();
map.put("add", new MyFunction() {
public int calculate(int a, int b) {
return a + b;
});
map.put("multiply", new MyFunction() {
public int calculate(int a, int b) {
return a * b;
});
You may do it by using reflection. Consider the following code.
Map<String, Method> map = new HashMap<String, Method>();
SomeClass obj = new Someclass();
Method method = null;
try {
method = obj.getClass().getMethod("someMethod", param1.class, param2.class, ...);
} catch (SecurityException e) {
} catch (NoSuchMethodException e) {
}
map.put("someMethod", method);
try {
(map.get("someMethod")).invoke(obj, arg1, arg2, ...);
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
The method to be called inside SomeClass
public class SomeClass {
// Other class contents
public void someMethod() {
// Target method contents
}
}
A sample code using the above logic:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
public class Temp3 {
public static void main(String[] args) {
Map<String, Method> map = new HashMap<String, Method>();
Temp3 obj = new Temp3();
Method method = null;
try {
method = obj.getClass().getMethod("someMethod");
} catch (SecurityException e) {
} catch (NoSuchMethodException e) {
}
map.put("someMethod", method);
try {
(map.get("someMethod")).invoke(obj);
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
public void someMethod() {
System.out.println("**************************************");
}
}
If you want that behavior you should define your own class.
The class should implement, at least, the following interface:
interface ApplicativeMap<K,V>{
void put(K key, Function<V> producer);
V get(K key);
where Function<V>
should be an interface exposing the an V apply()
method.
Your implementation should look something similar to this:
class ApplicativeMapImplementation implements ApplicativeMap{ private Map> functions;
public void put(K key, Function<V> producer){
functions.put(key, producer);
}
public V get(K key){
if(functions.containsKey(key)){
return functions.get(key).apply();
} else{
throw new NoSuchElementException();
}
}