<< First Java Program | Home

Lamda Expression Java 8

Lambda Expression Part 1

Lambda expression enables functional programming in java 8. We can consider  lamda as anonymous method in java.

For first lamda expression. Create a Interface MyCalculation


 

package com.lambdalearning;

 

public interface MyCalculation {

 

          public int opTwoNum(int x,int y);

}

 


Create class for lamda FewLambdas expression example

 


package com.lambdalearning;

 

public class FewLambdas {

 

          public static void main(String[] args) {

                   // TODO Auto-generated method stub

 

                   // we use lambda function for inline calculation some time they are more

                   // efficient and code readability is more

 

                   // sum of Two numbers

 

                   MyCalculation op = (x, y) -> x + y;

 

                   System.out.println(op.opTwoNum(12, 13));

 

                   op = (x, y) -> x - y;

                   System.out.println(op.opTwoNum(12, 13));

 

                   op = (x, y) -> x * y;

                   System.out.println(op.opTwoNum(12, 13));

 

                   op = (x, y) -> x / y;

                   System.out.println(op.opTwoNum(12, 13));

          }

 

}

 

 

 

Read more...




Add a comment Send a TrackBack