File tree Expand file tree Collapse file tree
Exploring-Java-8/src/in/explore/core/java8 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 66 * 1. We can directly instantiate Thread 2. Or, we can provide implementation of
77 * Runnable Interface and override run() method.
88 *
9- * @author prasingh26
9+ * @author prashantsingh
1010 *
1111 */
1212public class BasicThread {
@@ -35,6 +35,7 @@ public static void main(String args[]) throws InterruptedException {
3535 * turn for processing , till myThread is completed.
3636 */
3737 myThread .join ();
38+
3839 myExtendedThread .join ();
3940 myExtendedThread2 .join ();
4041
Original file line number Diff line number Diff line change 1+ package in .explore .core .java8 .lambda ;
2+
3+ /**
4+ * Custom Functional Interface, annotated with @FunctionalInterface and has only
5+ * one abstract method which can be referenced/implemented using Lambda
6+ * Expressions
7+ *
8+ * @author prashantsingh
9+ * @since 05/01/2018
10+ */
11+
12+ @ FunctionalInterface
13+ public interface Adder {
14+
15+ /**
16+ * This method adds two numbers passed as parameters
17+ *
18+ * @param a
19+ * int param
20+ * @param b
21+ * int param
22+ * @return sum of parameters
23+ * @author prashantsingh
24+ */
25+ public int add (int a , int b );
26+
27+ /**
28+ * Since Java 8 , an interface can define/ hold static methods as well.
29+ *
30+ * @param a
31+ * int param
32+ * @param b
33+ * int param
34+ * @param c
35+ * int param
36+ * @return sum of parameters
37+ * @author prashantsingh
38+ */
39+ public static int addThreeNumbers (int a , int b , int c ) {
40+ return a + b + c ;
41+ }
42+
43+ }
Original file line number Diff line number Diff line change 1+ package in .explore .core .java8 .lambda ;
2+
3+ /**
4+ *
5+ * Class to demonstrate usage of a custom functional interface
6+ *
7+ * @author prashantsingh
8+ * @since 05/01/2018
9+ *
10+ */
11+ public class BasicLambdaExpression2 {
12+
13+ public static void main (String args []) {
14+
15+ /**
16+ * To use Adder interface , we need to provide an implementation of the abstract
17+ * methods defined by it .
18+ */
19+
20+ // Approach #1 : Creating an Anonymous class to provide implementation of add
21+ // method
22+ Adder add = new Adder () {
23+ // anonymous class
24+ @ Override
25+ public int add (int a , int b ) {
26+ return a + b ;
27+ }
28+
29+ };
30+
31+ // Approach #2 : Creating a lambda expression to implement add method
32+
33+ // Here we are also holding reference of lambda expression , which can
34+ // again be used somewhere else if required.
35+
36+ Adder lambdaAdd = (a , b ) -> (a + b );
37+
38+ System .out .println (" Addition Result Anonymous class ==>" + add .add (10 , 20 ));
39+ System .out .println (" Addition Result Lambda Function ==>" + lambdaAdd .add (10 , 20 ));
40+
41+ }
42+
43+ }
Original file line number Diff line number Diff line change 1010 *
1111 */
1212public class BasicStreamExample1 {
13-
13+
1414 public static void main (String args []) {
15-
1615 // Filtering content from the list
17-
1816 List <Integer > list = createList ();
1917 // Dumping all even number from the stream to the console
2018 System .out .println (" Even Numbers from the stream" );
2119 list .stream ().filter (i -> i % 2 == 0 ).forEach (System .out ::println );
22-
2320 System .out .println (" Double of all numbers" );
2421 // Double every number and print on the console
2522 list .stream ().map (i -> i * 2 ).forEachOrdered ((s ) -> System .out .println (s ));
26-
2723 }
2824
2925 /**
@@ -34,7 +30,7 @@ public static void main(String args[]) {
3430 */
3531 private static List <Integer > createList () {
3632 List <Integer > list = new ArrayList <Integer >();
37- for (int i = 1 ; i <= 20 ; i ++) {
33+ for (int i = 1 ; i <= 20 ; i ++) {
3834 list .add (i );
3935 }
4036 return list ;
You can’t perform that action at this time.
0 commit comments