3

I have a function in class ABC whose definition is roughly as below

Boolean getBool(Value val1, Value val2) {
  if (val1.getVal() == val2.getVal()) {
    return true;
  }
  return false;
}

How can I pass this method as reference to another method, let's say operate() and call it from there.

String operate(<<<Pass getBool method here>>>, Param1) {
  val1 = some computations on Param1
  val2 = some other computations on Param2
  Boolean value = <<<<<Call getBool with val1 and val2 here>>>>>
  if (value) { return "Yes"; }
  else { return "No"; }
}

Please help!! I have read a couple of solutions but they are either passing a method by reference with parameters, but void return type, or, passing a method by reference without any parameters and some return type. I want to pass a function by reference with parameters and having some return type.

Sumit
  • 55
  • 2
  • 4
  • `Boolean value = getBool(val1, val2);`? – nbokmans Jan 17 '17 at 13:24
  • You can use the `BiPredicate` type. Here's a list: https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html Alternatively, you could have defined your own functional interface. – Jorn Vernee Jan 17 '17 at 13:24
  • The easy way is just to wrap a call to the method in a lambda, then pass the lambda. Java has a `::` iirc that can be used as well to get a reference to method. – Carcigenicate Jan 17 '17 at 13:25
  • First of all, Java is always pass-by-value (read more here: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). Here you're gonna learn how to pass function as paramter: http://stackoverflow.com/questions/4685563/how-to-pass-a-function-as-a-parameter-in-java – Piotr Podraza Jan 17 '17 at 13:25
  • While this question is technically a duplicate, reading those other questions and answers you tend to drown in pre-Java-8 stuff that you don’t want if you can use Java 8 lambdas. I think you get the best answer from [the tutorial on Lambda expressions](http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html). – Ole V.V. Jan 17 '17 at 13:59

2 Answers2

3

Your method can be represented by the Java 8 functional interface BiFunction<T, U, R>, since it takes two arguments and returns a value.

String operate(BiFunction<Value,Value,Boolean> function, TypeOfParam1 param1, TypeOfParam2 param2) {
    val1 = some computations on param1
    val2 = some other computations on param2
    Boolean value = function.apply(val1,val2);
    return value ? "Yes" : "No";
}

And call operate with a method reference :

ABC abcInstance = ...
String boolString = operate (abcInstance::getBool, param1, param2);
Eran
  • 387,369
  • 54
  • 702
  • 768
1

Use BiFunction class :

public class Test {

private static Boolean getBool(int val1, int val2) {
    if (val1 == val2) {
        return true;
    }
    return false;
}

private static String operate(BiFunction<Integer, Integer, Boolean> fun, int param1, int param2) {
    int val1 = param1;
    int val2 = param2;
    Boolean value = fun.apply(val1, val2);
    if (value) {
        return "Yes";
    } else {
        return "No";
    }
}

public static void main(String[] args) {
    operate(TestStaticReference::getBool, 1, 2);
    }

}
Rohit Gulati
  • 542
  • 3
  • 15