1

Given this method:

private static Integer getVal(Integer a, Integer b){ return a + b; } 

which can be called as a lambda:

a -> getVal(1, 2) 

Is there anyway of turning this into a method reference, something like:

Class::getVal 

Thanks

1
  • 4
    Why do you think you need this? Commented Dec 27, 2017 at 8:44

3 Answers 3

2

Well, if you are passing constants to the method call, you can create another method that calls the original method:

private static Integer getVal (Integer a) { return getVal(1,2); } 

then you can use method reference for the second method.

i.e. you can change

a -> getVal(1, 2) 

to

ClassName::getVal 

That said, it doesn't make much sense.

P.S., it's not clear what's the purpose of a in your lambda expression, since you are ignoring it.

In general you can pass a method reference of a given method if it matches the signature of the single method of the required functional interface.

Example:

public static Integer apply (BinaryOperator<Integer> op, Integer a, Integer b) { return op.apply(a,b); } 

Now you can call:

apply(ClassName::getVal) 

with your original method.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. That's really the question - how to pass the integer values to the lambda, like: (val1, val2) -> getVal(val1, val2)
@user1052610 It depends where you are passing your method reference to. If you are passing it to a method that requires a functional interface having a method with 2 arguments, you can pass your original method.
It's being passed to the second argument of Map.computeIfAbsent(). So what I'd like is map.computeIfAbsent("xxx", Class:getVal)
@user1052610 The second argument of computeIfAbsent() takes a Function<? super K, ? extends V>, so it requires a method with a single argument, not two.
What if the first argument was an object wrapper which contains two values. Would it be possible to pass that way?
|
2

Here is an example.

interface Operator { int operate(int a, int b); } class Calc { public static int add(int a, int b) { return a + b; } } class Main { public static void main(String[] args) { // using method reference int result = operate(1, 2, Calc::add); // using lambda int result = operate(1, 2, (a, b) -> Calc.add(a, b)); } static int operate(int a, int b, Operator operator) { return operator.operate(a, b); } } 

You need a functional interface to use method reference (In this example Operator). And you also need a method which accepts an instance of the functional interface as its parermater (In this example operate(int a, int b, Operator operator).

UPDATE

If you need an object wrapper, just change the operate method to

static int operate(ObjectWrapper wrapper, Operator operator) { return operator.operate(wrapper.getA(), wrapper.getB()); } 

And then call the operate method:

int result = operate(wrapper, Calc::add); 

Comments

1

getVal() will only be usable as a method reference, in places where a functional interface of an applicable type is expected, such as BiFunction or IntBinaryOperator, or a custom functional interface (as in the answer of zhh)

Example:

 public static void main(String[] args) { Integer result1 = calculate(1, 2, Second::getVal); Integer result2 = calculateAsInt(1, 2, Second::getVal); } private static Integer getVal(Integer a, Integer b){ return a + b; } private static Integer calculate(Integer a, Integer b, BinaryOperator<Integer> operator) { return operator.apply(a, b); } private static int calculateAsInt(int a, Integer b, IntBinaryOperator operator) { return operator.applyAsInt(a, b); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.