-
Notifications
You must be signed in to change notification settings - Fork 10
/
Random.java
52 lines (44 loc) · 1.49 KB
/
Random.java
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
49
50
51
52
// Java program to demonstrate working of lambda expressions
public class Random
{
// operation is implemented using lambda expressions
interface FuncInter1
{
int operation(int a, int b);
}
// sayMessage() is implemented using lambda expressions
// above
interface FuncInter2
{
void sayMessage(String message);
}
// Performs FuncInter1's operation on 'a' and 'b'
private int operate(int a, int b, FuncInter1 fobj)
{
return fobj.operation(a, b);
}
public static void main(String args[])
{
// lambda expression for addition for two parameters
// data type for x and y is optional.
// This expression implements 'FuncInter1' interface
FuncInter1 add = (int x, int y) -> x + y;
// lambda expression multiplication for two parameters
// This expression also implements 'FuncInter1' interface
FuncInter1 multiply = (int x, int y) -> x * y;
// Creating an object of Test to call operate using
// different implementations using lambda Expressions
Test tobj = new Test();
// Add two numbers using lambda expression
System.out.println("Addition is " +
tobj.operate(6, 3, add));
// Multiply two numbers using lambda expression
System.out.println("Multiplication is " +
tobj.operate(6, 3, multiply));
// lambda expression for single parameter
// This expression implements 'FuncInter2' interface
FuncInter2 fobj = message ->System.out.println("Hello "
+ message);
fobj.sayMessage("hello");
}
}