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

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

AnonymousInnerClassVsLambdas.java

+30
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

+19
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

+33
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

+10
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

+23
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

+24
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+

GoodiesWithLambdas.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.bootrcamp.demo;
2+
3+
interface MyFunctionalInterface{
4+
5+
int returnValue(int number);
6+
}
7+
8+
9+
public class GoodiesWithLambdas {
10+
11+
public static void main(String[] args) {
12+
13+
MyFunctionalInterface myFunctionalInterface = number -> number*number;
14+
15+
System.out.println(myFunctionalInterface.returnValue(5));;
16+
17+
}
18+
}

JDKDefinedFunctionalInterfaces.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.bootrcamp.demo;
2+
3+
import java.util.function.*;
4+
5+
public class JDKDefinedFunctionalInterfaces {
6+
7+
public static void main(String[] args) {
8+
Consumer<String> consumer = a -> System.out.println(a);
9+
consumer.accept("54");
10+
Predicate<Integer> isEven = a -> a % 2 == 0;
11+
System.out.println(isEven.test(9));
12+
Supplier<Integer> supplier = () -> 9;
13+
System.out.println(supplier.get());
14+
Function<Integer, Integer> getSquare = a -> a * a;
15+
System.out.println(getSquare.apply(5));
16+
BiFunction<Integer,Integer,Integer> multiply = (a,b)->a*b;
17+
System.out.println(multiply.apply(2,3));;
18+
}
19+
}

JDKFunctionalInterfaceDemo.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.bootrcamp.demo;
2+
3+
public class JDKFunctionalInterfaceDemo {
4+
5+
static void someStaticMethod( int i){
6+
System.out.println("Hello from JDKFunctionalInterfaceDemo static method::"+i);
7+
}
8+
9+
void someNonStaticMethod(int i){
10+
System.out.println("Hello from JDKFunctionalInterfaceDemo non static method::"+i);
11+
}
12+
13+
14+
15+
public static void main(String[] args) throws InterruptedException {
16+
17+
18+
new Thread(() -> {
19+
System.out.println("Another Thread ::::"+Thread.currentThread().getId());
20+
}).start();
21+
22+
System.out.println("Main Thread ::::"+Thread.currentThread().getId());
23+
24+
25+
}
26+
}

LambdasDemo.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.bootrcamp.demo;
2+
3+
4+
public class LambdasDemo {
5+
public static void main(String[] args) {
6+
7+
Operation operation =(int a, int b)-> {
8+
System.out.println(a+b);
9+
};
10+
11+
operation.perform(1,2);
12+
}
13+
}

MethodReferenceDemo.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.bootrcamp.demo;
2+
3+
interface MyReferenceInterface{
4+
void display(int a);
5+
}
6+
7+
public class MethodReferenceDemo {
8+
public static void main(String[] args) {
9+
MyReferenceInterface myReferenceInterface = (i)-> System.out.println(i);
10+
myReferenceInterface.display(5);
11+
myReferenceInterface = JDKFunctionalInterfaceDemo::someStaticMethod;
12+
myReferenceInterface.display(2);
13+
myReferenceInterface = new JDKFunctionalInterfaceDemo()::someNonStaticMethod;
14+
myReferenceInterface.display(1);
15+
16+
MethodReferenceDemo methodReferenceDemo = new MethodReferenceDemo();
17+
}
18+
}

OptionalDemo.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.bootrcamp.demo;
2+
3+
import java.util.Arrays;
4+
5+
public class OptionalDemo {
6+
7+
public static void main(String[] args) {
8+
9+
System.out.println(Arrays.asList(35,87)
10+
.stream()
11+
.filter(e->e%2==0)
12+
.findFirst().orElse(-1));
13+
14+
15+
System.out.println(Arrays.asList(35,87)
16+
.stream()
17+
.filter(e->e%2==0)
18+
.findFirst().orElseGet(()->{
19+
///Configuration environment
20+
return -234;
21+
}));
22+
}
23+
24+
25+
}

ParallelStreamVsNormalStream.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.bootrcamp.demo;
2+
3+
import java.util.stream.Collectors;
4+
import java.util.stream.IntStream;
5+
6+
public class ParallelStreamVsNormalStream {
7+
8+
public static void main(String[] args) {
9+
10+
long startTime = System.currentTimeMillis();
11+
IntStream
12+
.rangeClosed(1,10)
13+
.parallel()
14+
.filter(e->{
15+
try {
16+
Thread.sleep(1000l);
17+
} catch (InterruptedException ex) {
18+
ex.printStackTrace();
19+
}
20+
return e%2==0;} )
21+
.map(e->{ try {
22+
Thread.sleep(1000l);
23+
} catch (InterruptedException ex) {
24+
ex.printStackTrace();
25+
}
26+
return e*34;})
27+
.boxed()
28+
.collect(Collectors.toList());
29+
long endTime = System.currentTimeMillis();
30+
31+
System.out.println(endTime-startTime);
32+
33+
34+
35+
36+
}
37+
}

ParallelStreamsDemo.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.bootrcamp.demo;
2+
3+
import java.util.Arrays;
4+
import java.util.stream.IntStream;
5+
6+
public class ParallelStreamsDemo {
7+
8+
public static void main(String[] args) {
9+
10+
IntStream.rangeClosed(0,50)
11+
.parallel()
12+
.forEach(System.out::println);
13+
14+
System.out.println("--------------");
15+
16+
Arrays.asList(1,2,3,4,5,6,7,8,9,10)
17+
.parallelStream()
18+
.forEach(System.out::println);
19+
20+
21+
}
22+
}

0 commit comments

Comments
 (0)