Lambda in Java

Lambda Expression

Lambda expressions are introduce in Java 8 and are touted to be the biggest feature of Java 8.
Lambda expressions facilitates functional programming,and simplifies the development a lot.

Syntax

A lambda expression is characterized by the following syntax.

parameter -> expression body

Following are the important characteristics of a lambda expression.

  • Optional type declaration–No need to declare the type of parameter.The compiler can inference the same from the
    value of the parameter.
  • Optional parenthesis() around parameter–No need to declare a single parameter in parenthesis.For multiple
    parameters,parentheses are required.
  • Optional curly braces{}–No need to user curly braces in expression body if the body contains a single
    statement.
  • Optional return keyword–The compiler automatically returns the value if the body has a single expression to
    return the value.Curly braces are required to indicate that expression return a value.

Lambda Expressions Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class LambdaTest {
private int operate(int a, int b, MathOperation m) {
return m.operation(a, b);
}

public static void main(String[] args) {
LambdaTest test = new LambdaTest();

// with type declaration
MathOperation addtion = (int a, int b) -> a + b;

// without type declaration
MathOperation subtraction = (a, b) -> a - b;

// with return statement along with curly braces
MathOperation multiplication = (int a, int b) -> {
return a * b;
};

// without return statement and without curly braces
MathOperation division = (a, b) -> a / b;

System.out.println("\33[33m");//set the yellow foreground color.
System.out.println("10 + 5 =" + test.operate(10, 5, addtion));
System.out.println("10 - 5 =" + test.operate(10, 5, subtraction));
System.out.println("10 * 5 =" + test.operate(10, 5, multiplication));
System.out.println("10 / 5 =" + test.operate(10, 5, division));

// without parenthesis
GreetingService gs_without = message -> System.out.println(message);

// with parenthesis
GreetingService gs_with = (message) -> System.out.println(message);

gs_without.sayMessage("parameter without parenthesis.");
gs_with.sayMessage("parameter with parenthesis.");

}
}

interface MathOperation {
int operation(int a, int b);
}

interface GreetingService {
void sayMessage(String message);
}

Verify the result

Compile the class using javac compiler as follows:

$javac LambdaTest.java && java LambdaTest

It should produce the following output:

1
2
3
4
5
6
10 + 5 =15
10 - 5 =5
10 * 5 =50
10 / 5 =2
parameter without parenthesis.
parameter with parenthesis.

Following are the important points to be considered in the above example.

  • Lambada expressions are used primary to define inline implementations of a functional interface,an interface
    with a single methode only.
  • Lambda expressions eliminates the need of anonymous class and gives a very simple powerful functional programming
    capability to Java.
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022-2023 Ataraxia

请我喝杯咖啡吧~