Skip to content

Commit 8140410

Browse files
author
b
committed
Added Examples
1 parent 0090ded commit 8140410

5 files changed

+94
-5
lines changed

src/com/advancedjava/LinkedHashMapExample.java renamed to src/com/advancedjava/Ex6LinkedHashMapExample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.Map;
77

88

9-
public class LinkedHashMapExample {
9+
public class Ex6LinkedHashMapExample {
1010

1111
public static void main(String[] args) {
1212
LinkedHashMap<String, Integer> phoneBook = new LinkedHashMap<>(2, 0.75f, false);

src/com/advancedjava/FunctionalInterfaceExample.java renamed to src/com/advancedjava/Ex7FunctionalInterfaceExample.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,27 @@ interface GreetingMessage {
99
abstract void greet(String name);
1010
}
1111

12-
public class FunctionalInterfaceExample {
12+
public class Ex7FunctionalInterfaceExample {
1313

1414
public static void main(String[] args) {
1515

16-
1716
GreetingMessage gm = new GreetingMessage() {
1817
@Override
1918
public void greet(String name) {
2019
System.out.println("Assalamu Alaikum " + name);
2120
}
2221
};
2322

24-
2523
gm.greet("Bodrul Amin");
24+
25+
GreetingMessage gm2 = (String name) -> {
26+
27+
System.out.println("Assalamu Alaikum " + name);
28+
};
29+
30+
gm2.greet("Bodrul");
31+
32+
2633
}
2734

2835
}

src/com/advancedjava/LambdaExample.java renamed to src/com/advancedjava/Ex8LambdaExample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77

8-
public class LambdaExample {
8+
public class Ex8LambdaExample {
99

1010
public static void main(String[] args) {
1111

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.advancedjava;
2+
3+
import com.advancedjava.*;
4+
import java.util.HashMap;
5+
6+
class Square {
7+
8+
int sideLength;
9+
10+
public Square(int sideLength) {
11+
this.sideLength = sideLength;
12+
}
13+
14+
public int calculateArea() {
15+
return sideLength * sideLength;
16+
}
17+
}
18+
19+
@FunctionalInterface
20+
interface Shapes{
21+
public abstract int getArea(Square person);
22+
}
23+
24+
25+
public class Ex9MethodReference {
26+
27+
public static void main(String[] args) {
28+
Square s = new Square(4);
29+
Shapes shapes = ((square) -> {
30+
return square.calculateArea(); //To change body of generated lambdas, choose Tools | Templates.
31+
});
32+
33+
Shapes altShapes = Square::calculateArea;
34+
35+
System.out.println(shapes.getArea(s));
36+
System.out.println(altShapes.getArea(s));
37+
38+
39+
}
40+
41+
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.advancedjava;
2+
3+
import com.advancedjava.*;
4+
import java.util.HashMap;
5+
6+
class Square {
7+
8+
int sideLength;
9+
10+
public Square(int sideLength) {
11+
this.sideLength = sideLength;
12+
}
13+
14+
public int calculateArea() {
15+
return sideLength * sideLength;
16+
}
17+
}
18+
19+
@FunctionalInterface
20+
interface Shapes{
21+
public abstract int getArea(Square person);
22+
}
23+
24+
25+
public class Exx10Streams {
26+
27+
public static void main(String[] args) {
28+
Square s = new Square(4);
29+
Shapes shapes = ((square) -> {
30+
return square.calculateArea(); //To change body of generated lambdas, choose Tools | Templates.
31+
});
32+
33+
Shapes altShapes = Square::calculateArea;
34+
35+
System.out.println(shapes.getArea(s));
36+
System.out.println(altShapes.getArea(s));
37+
38+
39+
}
40+
41+
}

0 commit comments

Comments
 (0)