How to invoke different methods from parameters in Java?

injoy :

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

Ousmane D. :

BiFunction represents a function that accepts two arguments and produces a result, so you may want to look into that here.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=77667&siteId=1