Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java method reference

Tags:

java

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> :)

like image 783
Lev Avatar asked Apr 10 '12 08:04

Lev


People also ask

What is a method reference in Java?

Java provides a new feature called method reference in Java 8. Method reference is used to refer method of functional interface. It is compact and easy form of lambda expression. Each time when you are using lambda expression to just referring a method, you can replace your lambda expression with method reference.

Which is example of method reference?

sort(stringArray, String::compareToIgnoreCase); The equivalent lambda expression for the method reference String::compareToIgnoreCase would have the formal parameter list (String a, String b) , where a and b are arbitrary names used to better describe this example. The method reference would invoke the method a.

Why method reference is used?

A method reference is an alternative to creating an instance of a reference type. Among several other uses, method references can be used to create a functional interface instance (that is, an instance of a functional interface type).

What is static method reference in Java?

A static method reference refers to a static method in a specific class. Its syntax is className::staticMethodName , where className identifies the class and staticMethodName identifies the static method. An example is Integer::bitCount .


1 Answers

Now that Java 8 is out I thought I'd update this question with how to do this in Java 8.

package com.sandbox;

import java.util.HashMap;
import java.util.Map;

public class Sandbox {
    public static void main(String[] args) {
        Map<Class, Runnable> dict = new HashMap<>();

        MyClass1 myClass1 = new MyClass1();
        dict.put(MyClass1.class, myClass1::sideEffects);

        MyClass2 myClass2 = new MyClass2();
        dict.put(MyClass2.class, myClass2::sideEffects);

        for (Map.Entry<Class, Runnable> classRunnableEntry : dict.entrySet()) {
            System.out.println("Running a method from " + classRunnableEntry.getKey().getName());
            classRunnableEntry.getValue().run();
        }
    }

    public static class MyClass1 {
        public void sideEffects() {
            System.out.println("MyClass1");
        }
    }

    public static class MyClass2 {
        public void sideEffects() {
            System.out.println("MyClass2");
        }
    }

}
like image 183
Daniel Kaplan Avatar answered Sep 29 '22 09:09

Daniel Kaplan