Skip to content

Commit 446f472

Browse files
committed
Collection and Anonymous Inner Class
Lambda Expressions , Anonymous Inner Class and Collections
1 parent 7f7739d commit 446f472

4 files changed

+284
-36
lines changed

src/com/know/lambda/Lesson_04_Anonymous_Inner_Class_and_Lambda_Expressions.java

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.know.lambda;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.Comparator;
7+
import java.util.List;
8+
import java.util.Set;
9+
import java.util.TreeSet;
10+
import java.util.function.Consumer;
11+
12+
/**
13+
* Any where there is Functional Interface we can use
14+
* Lambda Expression.
15+
* Since Java Collection framework classes support
16+
* comparator functional interface we can optimize code
17+
* with Lambda expressions.
18+
*
19+
* @author KnowGroup
20+
*/
21+
public class Lesson_04_Collection_And_Lambda_Expressions {
22+
23+
public static void main(String[] arg){
24+
25+
Consumer<Object> log = o -> System.out.println(o.toString());
26+
27+
/*
28+
Comparator is a Functional Interface since it
29+
contains only single abstract method
30+
int compare(T o1, T o2);
31+
Thus can hold Lambda Expressions
32+
*/
33+
// EXAMPLE 1
34+
Comparator<String> c = (obj1,obj2) -> obj1.compareTo(obj2);
35+
List<String> names = Arrays.asList("Raj","Zara","Aron","Mirza","Chao","Dravid");
36+
37+
log.accept(names);
38+
names.sort(c);
39+
log.accept(names);
40+
41+
// EXAMPLE 2
42+
Comparator<Integer> score_comparator = (s1,s2) -> s1 < s2 ? -1 : s1 > s2 ? 1 : 0 ;
43+
List<Integer> scores = Arrays.asList(90,12,80,-10,-20,30,40,100,150);
44+
45+
scores.sort(score_comparator);
46+
log.accept(scores);
47+
48+
49+
// EXAMPLE 3
50+
Integer id = 1000;
51+
List<Student> students = new ArrayList<>();
52+
students.add(new Student(++id, "Arya", 939.500));
53+
students.add(new Student(++id, "Zubia", 739.500));
54+
students.add(new Student(++id, "Mini", 839.500));
55+
students.add(new Student(++id, "Chini", 839.300));
56+
students.add(new Student(++id, "Bunny", 530.800));
57+
students.add(new Student(++id, "Danny", 838.900));
58+
students.add(new Student(++id, "Ranny", 455.700));
59+
students.add(new Student(++id, "Arya M", 839.100));
60+
61+
62+
log.accept(students);
63+
64+
// Sort by name
65+
Collections.sort(students,(s1,s2)->s1.name.compareTo(s2.name));
66+
log.accept(students);
67+
68+
// Sort by score
69+
Collections.sort(students,(s1,s2)->s1.score < s2.score ? 1 : s1.score > s2.score ? -1 : 0);
70+
log.accept(students);
71+
72+
73+
// EXAMPLE 4
74+
Set<Integer> random_nums = new TreeSet<>();
75+
// Math.random() return a random value between 0 till 1
76+
random_nums.add((int)(Math.random()*100));
77+
random_nums.add((int)(Math.random()*100));
78+
random_nums.add((int)(Math.random()*100));
79+
random_nums.add((int)(Math.random()*100));
80+
random_nums.add((int)(Math.random()*100));
81+
random_nums.add((int)(Math.random()*100));
82+
random_nums.add((int)(Math.random()*100));
83+
84+
// Natural Assending Order
85+
log.accept(random_nums);
86+
87+
Set<Integer> desc_random_nums
88+
= new TreeSet<>((i1,i2)->i1 < i2 ? 1 : i1 > i2 ? -1 : 0);
89+
desc_random_nums.addAll(random_nums);
90+
91+
log.accept(desc_random_nums);
92+
93+
}
94+
}
95+
class Student{
96+
Integer id;
97+
String name;
98+
Double score;
99+
100+
public Student(Integer id, String name, Double score) {
101+
this.id = id;
102+
this.name = name;
103+
this.score = score;
104+
}
105+
106+
@Override
107+
public String toString() {
108+
return "Student{" + "id=" + id + ", name=" + name + ", score=" + score + '}';
109+
}
110+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.know.lambda;
2+
3+
import static com.know.util.Utility.log;
4+
5+
/**
6+
* Anonymous Inner Class (AIC) is Name Less Inner Class
7+
* Lambda Expressions(LE) are NOT AIC
8+
*
9+
* LE != AIC
10+
*
11+
* @author KnowGroup
12+
*/
13+
public class Lesson_05_Anonymous_Inner_Class_and_Lambda_Expressions_Part_1 {
14+
15+
public static void main(String[] arg){
16+
// EXAMPLE 1
17+
18+
Thread t = new Thread(){
19+
// Write a class with NO name
20+
// This is Anonymous Inner Class
21+
@Override
22+
public void run() {
23+
for(int i = 0 ; i<10;i++){
24+
log("Child Thread");
25+
}
26+
}
27+
};
28+
29+
t.start();
30+
31+
for(int i = 0 ; i<10;i++){
32+
log("Parent Thread");
33+
}
34+
35+
36+
// Anonymous Inner class could be replaced with Inner Class
37+
// Since Runnable is Functional Interface then we can define it using
38+
// Lambda Expression
39+
40+
Runnable r = ()->{
41+
for(int i=0;i<10;i++){
42+
log("Lambda Child Thread ");
43+
}
44+
};
45+
Thread t1 = new Thread(r);
46+
t1.start();
47+
48+
Thread t2 = new Thread(()->{
49+
for(int i=0;i<10;i++)
50+
log("T2 Lambda child Thread");
51+
});
52+
t2.start();
53+
54+
// EXAMPLE 2
55+
/*
56+
57+
Anonymous Inner class(AIC) can have following types ::
58+
59+
1. AIC that extends Conceret Class
60+
2. AIC that extends Abstract Class
61+
3. AIC that extends Interface with any number of methods
62+
4. AIC that extends Interface with single abstract method (i.e. Functional Interface(FI))
63+
64+
Only, 4th AIC can be replaced with Lambda Expression, since it is the only one with FI.
65+
*/
66+
67+
// Concrete Class
68+
class A{
69+
protected void m(){
70+
log("A m()");
71+
}
72+
}
73+
// Type1 AIC
74+
A a = new A(){
75+
};
76+
77+
// Abstract Class
78+
abstract class B{
79+
abstract void m();
80+
}
81+
// Type2 AIC
82+
B b = new B(){
83+
void m(){
84+
log("B m()");
85+
}
86+
};
87+
88+
// Type3 AIC
89+
C c = new C(){
90+
public void m2() {}
91+
public void m3() {}
92+
public void m1() {}
93+
};
94+
95+
// Type4 AIC
96+
D d = new D() {
97+
public void m1() {}
98+
};
99+
100+
// Type4 AIC can only be replaced with Lambda Expressions
101+
D d_l = ()->{};
102+
103+
}
104+
// NON-Functional Interface
105+
interface C{
106+
void m1();
107+
void m2();
108+
void m3();
109+
}
110+
// Functional Interface
111+
@FunctionalInterface
112+
interface D{
113+
void m1();
114+
}
115+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.know.lambda;
2+
3+
import java.util.function.Consumer;
4+
5+
/**
6+
* Anonymous Inner Class (AIC) is Name Less Inner Class
7+
* Lambda Expressions(LE) are NOT AIC
8+
*
9+
* LE != AIC
10+
*
11+
* @author KnowGroup
12+
*/
13+
public class Lesson_05_Anonymous_Inner_Class_and_Lambda_Expressions_Part_2 {
14+
15+
public static void main(String[] arg){
16+
Consumer<Object> log = o -> System.out.println(o.toString());
17+
/*
18+
Anonymous Inner Class can declare local variables just like any class
19+
and this.local_variable would refer to Latest inner class variable.
20+
*/
21+
22+
class A {
23+
int x = 888;
24+
public void m(){
25+
log.accept(this.x);
26+
}
27+
}
28+
29+
A a = new A(){
30+
int x = 999;
31+
public void m(){
32+
log.accept(this.x);//this would refer to 999
33+
}
34+
};
35+
a.m();
36+
37+
38+
39+
// Variables declared in Lambda Expressions are
40+
// LOCAL Variables NOT INSTANCE Variables
41+
42+
class Test {
43+
int x = 888;
44+
public void m2(){
45+
Intref i = () -> {
46+
int x = 999; // This is LOCAL variable to m1() method of Functional Interface
47+
log.accept("Lambda x "+this.x);// It is instance variable 888
48+
};
49+
i.m();
50+
}
51+
}
52+
53+
Test t = new Test();
54+
t.m2();
55+
}
56+
interface Intref{
57+
public void m();
58+
}
59+
}

0 commit comments

Comments
 (0)