Java Lambda tutorial – Basics

5 min read

Hey Guys!! In this post, we will be learning one of the most path breaking features introduced in JAVA 8, Lambdas.

What is Lambda?

  1. Lambda is equivalent to function(method) without a name.
  2. It is also referred as anonymous functions.
  3. Lambdas are not tied to any class or methods.
  4. Lambda can be assigned to a variable and passed around.

Why do we need Lambdas?

  1. Enables functional or declarative programming.
  2. Readable and concise code.
  3. Easier to use API and in built libraries.
  4. Enables support for parallel processing.

Syntax of Lambda

  1. () contains the parameters/arguments for a lambda function.
  2. -> Also known as arrow operator separates inputs from the processing logic.
  3. {} Braces holds the body which has processing logic.

OOPS vs Functional Programming

In Object oriented paradigm, everything is an object. All code blocks are associated with a class or object. In functional programs, variables and functions are the main elements of the code, while in object-oriented programs, objects and methods are the key elements. The programming model used in functional programming is a declarative programming model, while object-oriented programming uses the imperative programming model.

Below are some code snippets of lambdas which will give some insight of the syntax and working of lambda.

//OOPS version
public void greeting(){
        System.out.println(" Hey There!!");
    }
//Lambda version
greeting = () - > System.out.println(" Hey There!!");

As discussed earlier, in oops every code block is associated to a class. We can’t write code block in isolation, for example it is just performing an action and the method can’t be contained in a class. Lambdas can be used to achieve the same. Also, Lambdas can be used as first class object as well as values assigned to a type.

In the above example, greeting holds the lambda functions logic and this can be further passed in parameters of other functions.

//OOPS version
public int getDouble(int x){
         return x*2;
}
// Lambda version
getDouble = (int x) -> x*2; 

Above code snippet is an example of parameterized lambda. In comparison to OOPs version, lambdas version is concise and easy to write.

safeDivision = (int a, int b) -> {
    if(b==0) return 0;
    return a/b;
}

In the above code snippet, we enhanced the scope of function with an if condition.

Lambda as interface type

Lambda can be used within a class for implementing a interface without actually implementing a method in OOPs way. Below is an example of implementing an interface with lambda.

public class SampleLambdaImpl {

    public static void main(String[] args) {
        StringLength legthLambda = (String s) -> s.length();
        System.out.println(legthLambda.getLength("Hello World")); //11
    }

    interface StringLength{
        int getLength(String s);
    }
}

In the above code, lengthLambda is of type StringLength(interface) and the passed arguments mapped it to the getLength method of that interface. Compiler will take care of all the type inference depending on the arguments and return type of lambdas.

In the next tutorial, we will discuss functional interfaces in our ongoing lambda tutorials. Hope this will be helpful.

Happy learning!!

威而鋼