Skip to content

Commit d10e813

Browse files
Adding lamda, method reference and stream feature examples
1 parent badce5b commit d10e813

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

Java/Java8/LambdaFeature.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public void whatIsLamdaFeature() {
2121
MyInterface a ;
2222
// 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!");
2323
// As there is only 1 parameter - You can write like below without braces and type!
24+
// This is implemented using Consumer interface!
2425
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);
2526

2627
a.something("Developer");

Java/Java8/MethodRef.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package Java8;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Locale;
6+
7+
interface Parser {
8+
String parse(String s);
9+
}
10+
11+
class StringParser {
12+
public static String convert(String s) {
13+
if(s.length()<=3) {
14+
s = s.toUpperCase();
15+
}
16+
return s;
17+
}
18+
}
19+
class MyClass {
20+
public void print(String s, Parser p) {
21+
s = p.parse(s);
22+
System.out.println(s);
23+
}
24+
}
25+
public class MethodRef {
26+
public void howIsMethodPassedAsRef() {
27+
List<Integer> l = Arrays.asList(1,2,3,4);
28+
l.forEach(System.out::println);
29+
30+
//Eg: 2
31+
MyClass c = new MyClass();
32+
c.print("Bhavna", StringParser::convert);
33+
}
34+
}

Java/Java8/NewFeatures.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ public static void main(String[] args) {
1212

1313
LambdaFeature lf = new LambdaFeature();
1414
lf.whatIsLamdaFeature();
15+
16+
StreamsDemo sd = new StreamsDemo();
17+
sd.whatIsStream();
18+
19+
MethodRef mr = new MethodRef();
20+
mr.howIsMethodPassedAsRef();
1521
}
1622

1723
}

Java/Java8/StreamsDemo.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package Java8;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class StreamsDemo {
8+
void whatIsStream() {
9+
// It is used for processing the data. Can be used on once!
10+
11+
List<Integer> l = new ArrayList<>();
12+
for(int i=1; i<=100; i++) {
13+
l.add(i);
14+
}
15+
System.out.println(l.stream().filter(i->{
16+
if(i==2) return true;
17+
return false;
18+
}).findAny().orElse(0));
19+
}
20+
}

0 commit comments

Comments
 (0)