Brief Introduction to Lambda in java 8

Brief Introduction to Lambda in java 8  Nalinee Choudhary
Posted on May 22, 2017, 12:16 p.m.

learn java 8

Introduction

  1. Java Application running platform is generally multi-core/multi-processor.
  2. To leverage hardware advancement, application programmer had to rely on thread programming, concurrent libraries or fork-join. None of them makes life easy.
  3. Java 8 promises to make life easier for developers by introducing lambda & streams.
  4. Lambda expression is more like anonymous functions - functions without names
  5. Like functions, it has return types, arguments, body etc.
  6. Lambda is not associated with classes.
  7. Lambda is a way to pass a function as a parameter to function call like values.

 

Syntax

  1. Left side of '->' will contain all parameters. The right-hand side of it will have body.
  2. Left side of '->', Parameters with type information in (int x, int y) and many more options
  3. Right side of '->', Block of code in {} and many more options

    (Lambda Parameter List) -> { LambdaBody }

 

      Lambda expression takes one argument & returns multiple of 2 of the parameter

     (int a) -> { return a*2; }  

 

       Takes 2 argument & returns the sum.

     (int a, int b) -> { return a + b; }

 

      Another way to achieve the above result.

      the block is without a scope ( flower braces {} ), so no need to write return

      (int a, int b) -> a + b

 

      Nothing restricts to single line

(int a, int b) -> {

       int x = a + b;

       if ( x % 2 ){

          x = x * 2;

       }

       return x;

      }

 

       Taking no arguments and returning "Hello World"

       () -> "Hello World"

 

       Takes nothing & return type is void

       () -> { System.out.println("Dummy print"); }

 

       Type fewer parameters. All parameters should be typeless

       (a,b) -> {}

 

       Explicit Lambda Expression - Lambda Expression where explicitly type is mentioned.

      (int a, int b) -> a + b

 

       Implicit lambda Expression - Lambda Expression where no type is mentioned, a type is inferred from the way lambda is used. The expression that takes

      (a,b) -> {}

 

 

Hello World Program from Lambda

@FunctionalInterface

interface DoStudy {

                public String doEnjoyStudy(String name);

}

 

public class LambdaProg {

    //timeToStudy function takes function as an argument & data the function will work on as an argument           

                public static void timeToStudy(DoStudy d, String name){

                                System.out.println(d.doEnjoyStudy(name));

                }

                public static void main(String[] args) {

                    //Passing a lambda expression that takes one argument & returns string to function.

                                timeToStudy((String name) -> "Hello World from "+ name,"zekeLabs");

                }

}


Passionate about Technology and Training, she took up teaching technology courses at a very early stage of her career. She is a visiting faculty at some of the leading Technology Colleges. She has already trained more than 1000+ professionals and students from various colleges and companies including Ola, PayTM, TCS, Accenture, Infosys, IBM, Zomato and many more. Her area of interests are technologies like Python, Django, JAVA, Android, Web Application Development, Angular 2, UI Development, SQL/PLSQL and others




Keywords : Java-8 Functional-Programming


Recommended Reading