Java method reference

java

Solution

This is feature which is likely to be Java 8. For now the simplest way to do this is to use reflection.

public class TestClass {
    public void method(MyClass1 o) {
        // this method will be used for consuming MyClass1
    }

    public void method(MyClass2 o) {
        // this method will be used for consuming MyClass2
    }
}

and call it using

Method m = TestClass.class.getMethod("method", type);

Problem

I've some class with these methods: ``` public class TestClass { public void method1() { // this method will be used for consuming MyClass1 } public void method2() { // this method will be used for consuming MyClass2 } } ``` and classes: ``` public class MyClass1 { } public class MyClass2 { } ``` and I want `HashMap<Class<?>, "question">` where I would store (key: class, value: method) pairs like this ( class "type" is associated with method ) ``` hashmp.add(Myclass1.class, "question"); ``` and I want to know how to add method references to HashMap (replace "question"). p.s. I've come from C# where I simply write `Dictionary<Type, Action>` :)

Original source