Home » Java » How to invoke different methods from parameters in Java?

How to invoke different methods from parameters in Java?

Posted by: admin August 11, 2018 Leave a comment

Questions:

Assume that I have a method A and B from a class:

List<String> A(int start, int size);
List<String> B(int start, int size);

Now, I have another function that might use A or B. But before invoking them, I’ll do some logic first:

void C() {
 // lots of logic here
 invoke A or B
 // lots of logic here 
}

I want to pass method A or B as parameter of C, something like:

void C(Function<> function) {
 // lots of logic here
 invoke function (A or B)
 // lots of logic here 
}

However, I noticed that the Function class in Java can only accept one parameter (method A or B both have two parameters). Is there a workaround on this? (I don’t want to change the method A or B signatures).

Answers: