-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleLambda.java
More file actions
48 lines (36 loc) · 1.02 KB
/
SampleLambda.java
File metadata and controls
48 lines (36 loc) · 1.02 KB
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
48
// Java program to demonstrate simple lambda expressions
import java.util.ArrayList;
class SampleLambda {
public static void main(String args[]) {
ArrayList<Integer> l_arrList = new ArrayList<Integer>();
l_arrList.add(1);
l_arrList.add(2);
l_arrList.add(3);
l_arrList.add(4);
l_arrList.add(5);
l_arrList.add(6);
l_arrList.add(7);
l_arrList.add(8);
l_arrList.add(9);
l_arrList.add(10);
// using lambda expression, print all the elements from the list.
l_arrList.forEach(n -> System.out.println(n));
System.out.println();
// print the elements if the values is >2, using lambda expression
l_arrList.forEach(n -> {
if (n > 2) {
System.out.println(n);
}
});
// lambda expression print the message for single parameter
MessageDisplay msg = (String message) -> {
System.out.println("Welcome " + message);
};
System.out.println();
msg.message("Bdasan ");
}
}
// MessageDisplay is implemented using lambda expressions
interface MessageDisplay {
public void message(String p_msg);
}