Skip to content

Commit 1cc57b7

Browse files
committed
Anonymous Inner Class and Lambda
Anonymous Inner Class and Lambda
1 parent 92e21d7 commit 1cc57b7

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
/build/
1+
/build/
2+
/nbproject/private/

src/com/know/lambda/Lesson_01_FuntionalInterface.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,28 @@ interface Intef1{
4040

4141
public static void main(String[] arg){
4242
log("30 Square :: "+square.apply(30));
43+
44+
java.util.ArrayList<String> names = new java.util.ArrayList<String>();
45+
names.add("Raj");
46+
names.add("Jaz");
47+
names.add("Arya");
48+
names.add("Ajay");
49+
names.add("Zara");
50+
names.add("Madhu");
51+
52+
log("Names :: "+names);
53+
54+
/**
55+
* Functional Interface java.util.Comparator
56+
* can be defined using Lambda expression
57+
*/
58+
java.util.Comparator<String> c = (s1,s2) -> s1.compareTo(s2);
59+
names.sort(c);
60+
61+
log("Names Sorted :: "+names);
4362
}
4463

4564
public static void log(String msg){
4665
System.out.println(msg);
4766
}
48-
}
67+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.know.lambda;
7+
8+
import static com.know.util.Utility.log;
9+
10+
/**
11+
* Lambda Expressions(LE) are NOT Anonymous Inner Class
12+
* Anonymous Inner Class (AIC) is MORE powerful than LE
13+
* in a sense AIC are more flexible and uses multiple abstract methods and othe
14+
*
15+
*
16+
* @author KnowGroup
17+
*/
18+
public class Lesson_03_Anonymous_Inner_Class_and_Lambda_Expressions {
19+
20+
public static void main(String[] arg){
21+
Thread t = new Thread(){
22+
@Override
23+
public void run() {
24+
for(int i = 0 ; i<10;i++){
25+
log("Child Thread");
26+
}
27+
}
28+
};
29+
30+
t.run();
31+
32+
for(int i = 0 ; i<10;i++){
33+
log("Parent Thread");
34+
}
35+
}
36+
}

src/com/know/util/Utility.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.know.util;
7+
8+
/**
9+
*
10+
* @author KnowGroup
11+
*/
12+
public class Utility {
13+
14+
public static void log(String msg){
15+
System.out.println(msg);
16+
}
17+
}

0 commit comments

Comments
 (0)