-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_expression.java
More file actions
37 lines (29 loc) · 994 Bytes
/
Copy pathlambda_expression.java
File metadata and controls
37 lines (29 loc) · 994 Bytes
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
import java.util.*;
class Lambda_expression {
public static void main(String[] args){
int[] array = new int[5];
array[0] = 1;
array[1] = 5;
array[2] = 7;
array[3] = 3;
array[4] = 2;
for(int n : array){
System.out.println("nomal expression: " + n);
//normaly written code.
}
Arrays.stream(array).forEach(n-> {System.out.println("Lambda expression: " + n);});
}
}
/* lambda expression
The syntax of a lambda expression is
(parameters) -> {body}
Lambdas allow implementing functional interfaces (interfaces with only one abstract method) in a concise way.
If the body is a single expression, curly braces can be omitted: (x) -> x+1
Parameter types can be omitted if inferable from context: (x) -> x+1
in methods we don't need to write access modifier, name of method and return type.
public void hello(){
System.out.printl("Hello");
}
it become:-
()->system.out.println("Hello");
*/