0

I have a function functionAcceptsMethod which accepts a runnable method as a parameter, I want to call functionAcceptsMethod by passing a method with parameters.

when I call functionAcceptsMethod by passing without parameters its fine but how to pass a function with parameters.

here is an example

 private void testFun() {
        functionAcceptsMethod(this::funWithoutParams);
        functionAcceptsMethod(this::funWithParams); // this where I need to pass params
        //functionAcceptsMethod(() -> funWithParams("abcd")); // I tried this, is this the right 
         //way?
    }

private void funWithoutParams() {
    //do something
}

private void funWithParams(String testString) {
    //do something
}

private  void functionAcceptsMethod(Runnable method) {
    method.run();
}

2 Answers 2

2

The right way is to have a version of your functionAcceptsMethod method with the parameter:

private void <T> functionAcceptsMethod(Consumer<T> method, T argument) {
    method.accept(argument);
}

Note that Runnable#run is a functional interface without a parameter, so you cannot use it here. You have to use Consumer. Now you can do this:

functionAcceptsMethod(this::funWithParams, "abc");

Check out java.util.function for more possibilities.

0

Yes, The approach

functionAcceptsMethod(() -> funWithParams("abcd"));

is ok, if your purpose is to just pass method reference and get args in parameters of functionAcceptsMethod method then use Consumer<T> as function receiver argument

As you wanted an example:

public static <T> void functionAcceptsMethod(Consumer<T> consumer, T t){
    consumer.accept(t);
}

This is a generic version and will accept any method that has single input parameter. and an example of usage:

public void example(){
    // Automatic type inferring
    functionAcceptsMethod(this::func, "f");
    functionAcceptsMethod(this::func2, 0);
    // Explicit type specification
    functionAcceptsMethod<Float>(this::func3, 0f);
}
public void func(String s){
    System.out.println(s);
}
public void func2(int i){
    System.out.println(i);
}
public void func3(float f){
}
3
  • hi thank you for your response, method.run which I have in functionAcceptsMethod will not work to execute the function with parameters?
    – BRDroid
    Commented Apr 16, 2020 at 14:00
  • also can you suggest how to modify functionAcceptsMethod with Consumer please, I have never used Consumer before
    – BRDroid
    Commented Apr 16, 2020 at 14:01
  • for your first question, not at all you can't use that method you wrote for executing any function. I updated the answer and added some generic examples that u can use for any function with single parameters. if u want a general method for all type of functions you have 2 possible approaches: 1. getting method reference and parameters as array and using reflection two execute [not good approach, reflection is not efficient comparing to direct execution] 2. design an interface and have ur functions to follow the input/output signature of that. @BRDroid Commented Apr 16, 2020 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.