Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of lambda () -> { } in Java

I am looking at the following Stack Overflow answer: How to change Spring's @Scheduled fixedDelay at runtime

And in the code there is the following line:

schedulerFuture = taskScheduler.schedule(() -> { }, this);

I would like to know what the lambda () -> {} means in that code. I need to write it without using lambdas.

like image 596
Aliuk Avatar asked Sep 12 '18 10:09

Aliuk


People also ask

What does () -> in Java mean?

Basically, the -> separates the parameters (left-side) from the implementation (right side). The general syntax for using lambda expressions is. (Parameters) -> { Body } where the -> separates parameters and lambda expression body.

What does the arrow do in Java?

The Arrow OperatorThe left side specifies the parameters required by the expression, which could also be empty if no parameters are required. The right side is the lambda body which specifies the actions of the lambda expression.

Is lambda expression an object?

Yes, any lambda expression is an object in Java. It is an instance of a functional interface. We have assigned a lambda expression to any variable and pass it like any other object.

What is lambda expression in Java 8?

1 Java Lambda Expressions. Lambda Expressions were added in Java 8. A lambda expression is a short block of code which takes in parameters and returns a value. 2 Syntax. Expressions are limited. ... 3 Using Lambda Expressions. Lambda expressions can be stored in variables if the variable's type is an interface which has only one method.

What is argument list in lambda expression in Java?

Java Lambda Expression Syntax 1 Argument-list: It can be empty or non-empty as well. 2 Arrow-token: It is used to link arguments-list and body of expression. 3 Body: It contains expressions and statements for lambda expression.

How to store a lambda expression in a variable?

The lambda expression should have the same number of parameters and the same return type as that method. Java has many of these kinds of interfaces built in, such as the Consumer interface (found in the java.util package) used by lists. Use Java's Consumer interface to store a lambda expression in a variable:

When to use return keyword in Java lambda expression?

In Java lambda expression, if there is only one statement, you may or may not use return keyword. You must use return keyword when lambda expression contains multiple statements.


3 Answers

Its a Runnable with an empty run definition. The anonymous class representation of this would be:

new Runnable() {
     @Override public void run() {
          // could have done something here
     }
}
like image 58
Naman Avatar answered Oct 19 '22 08:10

Naman


Lamda expression is an anonymous function that allows you to pass methods as arguments or simply, a mechanism that helps you remove a lot of boilerplate code. They have no access modifier(private, public or protected), no return type declaration and no name.

Lets take a look at this example.

(int a, int b) -> {return a > b}

In your case, you can do something like below:

schedulerFuture = taskScheduler.schedule(new Runnable() {
     @Override 
     public void run() {
        // task details
     }
}, this);
like image 2
Prashant Goswami Avatar answered Oct 19 '22 07:10

Prashant Goswami


For lambdas:

Left side is arguments, what you take. Enclosed in () are all the arguments this function takes

-> indicates that it's a function that takes what's on the left and passes it on to the right for processing

Right side is the body - what the lambda does. Enclosed in {} is everything this function does

After you figure that out you only need to know that that construction passes an instance of matching class (look at what's the expected argument type in the schedule() call) with it's only method doing exactly the same as the lambda expression we've just analyzed.

like image 1
Deltharis Avatar answered Oct 19 '22 06:10

Deltharis