user3718619
user3718619

Reputation: 33

Lambda Parameter in java

My quest is how can I have an lambda expression as a parameter in a function?? My intention is, when I execute the sum method the parameter in method accept is taken and used by the object continuation (am I right??), well my doubt is how can I do to make this object continuation a lambda expression which uses the result of value1 + value2, and do something else after that??

Like X.sum(1,2,resultOfSum->{system.out.println(resultOfSum);});

(I've tried this and eclipse make it like an error)

Sorry my english is not so good.

This is my Code.

public class ExampleClass {

    public static void main(String[] args) {

        X x = new X();

        int nro ;

        x.sum(1,2,"AN EXAMPLE OF LAMBDA"));

    }

}

public class X{

    public void sum(int value1,int value2,Consumer<Integer> continuation)
    {
        continuation.accept(value2+value1);
    }}

Upvotes: 3

Views: 1572

Answers (2)

Makoto
Makoto

Reputation: 106500

Interestingly enough, your current lambda expression will work for this (barring the fix to the typo of system.out.println). The below is merely a more concise way to express it.

Effectively, you're writing out a method that takes in two values, produces one, and places it in some sort of consumer. That consumer could be a collection, some function that can handle the result, or another object.

Let's take lambdas out of the equation for a moment and write this out with an anonymous class.

x.sum(1, 2, new Consumer<Integer>() {
    @Override
    public void accept(final Integer value) {
        // what should go here?
    }
});

We need to tell this result where to go. We've got a few options:

  • A Collection (List, Set, etc)
  • A new reference object
  • A method reference that accepts the parameter

Let's take the simplest approach and simply print it out.

x.sum(1, 2, new Consumer<Integer>() {
    @Override
    public void accept(final Integer value) {
        System.out.println(value);
    }
});

Now, this makes some sense - we're taking the result of two numbers, adding them together, and then printing the result to the screen.

Let's put lambdas back into the mix. This can be expressed in terms of a lambda in this manner:

x.sum(1, 2, System.out::println);

Effectively, whatever resultant value we get back, we wish to then allow the println method to consume it.

Upvotes: 3

Dhruv Rai Puri
Dhruv Rai Puri

Reputation: 1375

I think I understand what you are trying to do. What is important to understand in your problem is - "what is the behavior we want to pass as Lambda?". In your case the answer to this question is "print the value of the summation passed to the Lambda/Consumer instance".

To achieve that we need to create a Lambda instance which does the printing and then pass it to the X.sum method. Written below is the code which achieves the same

//ExampleClass.java
import java.util.function.Consumer;
public class ExampleClass {
  public static void main(String[] args) {
    X x = new X();
    Consumer<Integer> consumerInstance=(value) -> System.out.println("Summation Result is ->value");
    x.sum(1, 2, consumerInstance);
  }
 }

//X.java
import java.util.function.Consumer;
public class X{
  public void sum(int value1,int value2,Consumer<Integer> continuation){
    continuation.accept(value1+value2);
  }
}

For basic understanding of Lambda Expressions you can refer this link - http://www.javabrahman.com/java-8/lambda-expressions-java-8-explained-examples/ .

(Note - www.javabrahman.com is a tech blog owned by me.)

Upvotes: 0

Related Questions