java lambda expressions example

Java 8 lambda expressions can be seen as a way of functional programming with java 8. A Java lambda expression is a function which can be created without creating any class or need any class. A lambda expression is treated as an object which can be passed and executed whenever we want.

Lambda expressions are used basically to define inline implementation of a functional interface (single method interfaces).
Lambda expression makes life easier eliminating the need of anonymous class.

Java lambda expressions example 1:

Hello World Example with Java 8 lambda expressions

Below is the example of the Hello World Print using Java Lambda expression, here you can point out some things such as
1. We don't need to declare the type of a parameter. The compiler take care of it.
2. We don't need to declare a single parameter in parenthesis.
public class LambdaExample1 {
   public static void main(String args[]){
      LambdaExample1 lambdaTest = new LambdaExample1();
      //In example below parenthesis is used
      HelloWorldService helloWorld1 = message ->
      System.out.println("Hello World " + message);
      //Example below is without parenthesis
      HelloWorldService helloWorld2 = (message) ->
      System.out.println("Hello World " + message);
   interface HelloWorldService {
      void sayLoud(String message);

Output :
Hello World John
Hello World Roger

Java lambda expressions example 2:

Java lambda expressions example for addition, subtraction,multiplication and division.
Following example explains there is no need to use curly braces in expression if the body has a single statement.
If the body has a single expression as a return value, then compiler automatically returns the value.

public class LambdaExpression2 {
   public static void main(String args[]){
      LambdaExpression2 lambdaExample2 = new LambdaExpression2();
      //Note : following example is with type declaration
      MathUtil subtraction = (int x, int y) -> x - y;
      //Note : following example is without type declaration
      MathUtil addition = (x, y) -> x + y;
      //Note : following example with return statement with curly braces
      MathUtil multiplication = (int x, int y) -> { return x * y; };
      //Note : following example without return statement and curly braces
      MathUtil division = (int x, int y) -> x / y;
      System.out.println("20 - 8 = " + lambdaExample2.getResult(20, 8, subtraction));
      System.out.println("15 + 5 = " + lambdaExample2.getResult(15, 5, addition));
      System.out.println("6 x 5 = " + lambdaExample2.getResult(6, 5, multiplication));
      System.out.println("25 / 5 = " + lambdaExample2.getResult(25, 5, division));
   interface MathUtil {
      int getResult(int a, int b);
   private int getResult(int a, int b, MathUtil mathUtil){
      return mathUtil.getResult(a, b);

20 - 8 = 12
15 + 5 = 20
6 x 5 = 30
25 / 5 = 5

Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment


Post a Comment