Skip to content

Commit badce5b

Browse files
Labda functions demo
1 parent d6d4cf0 commit badce5b

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

Java/Java8/ForEachLoop.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package Java8;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
5+
public class ForEachLoop {
6+
7+
public void whatIsForEachLoop() {
8+
List<Integer> arrayList = Arrays.asList(1,2,3,4,5);
9+
arrayList.forEach(i -> System.out.println(i)); //Lambda
10+
}
11+
}

Java/Java8/LambdaFeature.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package Java8;
2+
3+
interface MyInterface {
4+
public void something(String s);
5+
}
6+
7+
//class A implements MyInterface {
8+
// public void something() {
9+
// System.out.println("Hi, there is a better way of implementing me! So comment me");
10+
// }
11+
//}
12+
public class LambdaFeature {
13+
public void whatIsLamdaFeature() {
14+
// MyInterface a = new MyInterface() {
15+
// public void something(String s) {
16+
// System.out.println("So much code!" + s);
17+
// }
18+
// }
19+
20+
// Let's do the same above implementation using lamda!
21+
MyInterface a ;
22+
// a = (String s) -> System.out.println("Hey! This is lamda function and much better way of implementing me! If you want to see the magic, checkout what is created after you compile me!");
23+
// As there is only 1 parameter - You can write like below without braces and type!
24+
a = s-> System.out.println("Hey! This is lamda function and much better way of implementing me! If you want to see the magic, checkout what is created after you compile me, "+s);
25+
26+
a.something("Developer");
27+
}
28+
}

Java/Java8/NewFeatures.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
public class NewFeatures {
44
public static void main(String[] args) {
5-
Interfaces8 i1 = new Interfaces8();
6-
i1.act();
5+
// Interfaces8 i1 = new Interfaces8();
6+
// i1.act();
7+
// defaultInterfaceEg i2 = new defaultInterfaceEg();
8+
// i2.act();
79

8-
defaultInterfaceEg i2 = new defaultInterfaceEg();
9-
i2.act();
10+
ForEachLoop fe = new ForEachLoop();
11+
fe.whatIsForEachLoop();
12+
13+
LambdaFeature lf = new LambdaFeature();
14+
lf.whatIsLamdaFeature();
1015
}
1116

1217
}

0 commit comments

Comments
 (0)