Skip to content

Commit ddeb20f

Browse files
author
shimanshu sharma
committed
Lambda Function Example
0 parents  commit ddeb20f

21 files changed

+453
-0
lines changed

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

AnonymousInnerClassVsLambdas.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.bootrcamp.demo;
2+
3+
public class AnonymousInnerClassVsLambdas {
4+
int number = 21;
5+
6+
void display() {
7+
Operation operation1 = new Operation() {
8+
int number = 41;
9+
@Override
10+
public void perform(int a, int b) {
11+
System.out.println(this.number + a + b);
12+
}
13+
};
14+
15+
operation1.perform(1, 2);
16+
17+
Operation operation2 = (a, b) -> {
18+
System.out.println(this.number + a + b);
19+
};
20+
21+
operation2.perform(1,2);
22+
23+
}
24+
25+
26+
public static void main(String[] args) {
27+
new AnonymousInnerClassVsLambdas().display();
28+
}
29+
30+
}

ConstructorReferenceDemo.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.bootrcamp.demo;
2+
3+
4+
interface EmployeeFI {
5+
6+
Employee createEmployee(Integer id, Integer age, String name);
7+
}
8+
9+
public class ConstructorReferenceDemo {
10+
11+
public static void main(String[] args) {
12+
13+
EmployeeFI employeeFI = Employee::new;
14+
Employee employee = employeeFI.createEmployee(1, 32, "Peter Parker");
15+
System.out.println(employee);
16+
17+
18+
}
19+
}

DefaultMethodsDemo.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.bootrcamp.demo;
2+
3+
interface MyInterface1 {
4+
default void display() {
5+
System.out.println("MyInterface1");
6+
}
7+
}
8+
9+
interface MyInterface2 {
10+
default void display() {
11+
System.out.println("MyInterface2");
12+
}
13+
}
14+
15+
interface MyInterface3 extends MyInterface1, MyInterface2 {
16+
default void display() {
17+
MyInterface1.super.display();
18+
MyInterface2.super.display();
19+
System.out.println("MyInterface3");
20+
}
21+
}
22+
23+
public class DefaultMethodsDemo implements MyInterface3 {
24+
public void display() {
25+
MyInterface3.super.display();
26+
System.out.println("DefaultMethodsDemo");
27+
}
28+
29+
30+
public static void main(String[] args) {
31+
new DefaultMethodsDemo().display();
32+
}
33+
}

EffectivelyFinalDemo.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.bootrcamp.demo;
2+
3+
public class EffectivelyFinalDemo {
4+
5+
public static void main(String[] args) throws InterruptedException {
6+
final int value = 23;
7+
new Thread(() -> System.out.println(value)).start();
8+
Thread.sleep(1000L);
9+
}
10+
}

Employee.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.bootrcamp.demo;
2+
3+
public class Employee {
4+
5+
Integer id;
6+
Integer age;
7+
String name;
8+
9+
public Employee(Integer id, Integer age, String name) {
10+
this.id = id;
11+
this.age = age;
12+
this.name = name;
13+
}
14+
15+
@Override
16+
public String toString() {
17+
return "Employee{" +
18+
"id=" + id +
19+
", age=" + age +
20+
", name='" + name + '\'' +
21+
'}';
22+
}
23+
}

FunctionalProgramming.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.bootrcamp.demo;
2+
3+
@FunctionalInterface
4+
interface Operation {
5+
void perform(int a, int b);
6+
}
7+
8+
public class FunctionalProgramming {
9+
10+
static void performOperation(int a, int b, Operation operation) {
11+
operation.perform(a,b);
12+
}
13+
14+
public static void main(String[] args) {
15+
16+
performOperation(1, 2,(int a, int b)-> {System.out.println(a+b);});
17+
performOperation(1, 2,(int a, int b)-> {System.out.println(a-b);});
18+
performOperation(1, 2,(int a, int b)-> {System.out.println(a*2+b*2);});
19+
20+
}
21+
22+
}
23+
24+

0 commit comments

Comments
 (0)