Understanding Java Lamda Expression with examples

Lamda expression in Java was introduced in Java 8. This is one of my favorite, cool features of Java 8. This blog is intended to help understanding what is it, where and how can we use it.

What is lamda expression?

In simple words, Lamda expression in java is an anonymous function. It is defined with the parameter list and the function body.

Below is the syntax of lamda expression.


{parameter list} -> {function body}


So what is actually meant by this definition? Although we define an anonymous function here, this is actually like an implementation to a method in an interface.

Also, we can assign this lamda expression to a reference of the interface type.

See example below.

interface MyInterface {

    public String sayWelcome(String name);

}

public class LamdaExpSample {

  public static void main(String args[]) {
  
    MyInterface myIn = (name) -> {  
        return "Welcome " + name;
     };  
    }

}

As above, we have defined a Lamda expression to return a greet message. It has implemented the "sayWelcome" method and also assigns the expression to a reference called "myIn". This reference can be passed to anywhere MyInterface object is expected.

The lamda expression can be defined with no parameters, one parameter or multiple parameters. Also the function body can be wrapped with curly braces when there are multiple lines.

Note
One important thing to note here is that the interface should only have a single method. Because with lamda expression we don't specify the method name we implement. In Java 8 these interfaces are known as Functional interface. Those are normal java interfaces with a single method.

Below section explains the usage with further examples.

Where and how can we use Lamda expression? 

Prior to java 8, remember situations when we wanted to implement an interface and override a method? At such times, what we did was creating an anonymous inner class and overriding the method where we wanted to pass that interface object to. Now with Java 8, this is super easy. All we have to do is defining a Lamda expression there.


interface MyInterface {
    public String sayWelcome(String name);
}

class GreetProgram {
    public void greet(MyInterface  myIn) {
        System.out.println(myIn.sayWelcome("Lakmali"));
    }
}

public class LamdaExpSample {
    public static void main(String args[]) {
        GreetProgram greetProg = new GreetProgram ();
        greetProg.greet((name) -> {
            return "Welcome " + name;
        });
    }
}

As shown above, there is this GreetProgram class which has a method expecting an object from the MyInterface. So what we have done is defining an lamda expression as the interface object. Here I have done it inline. The same can be done by defining the lamda expression separately and passing the MyInterface reference to greet method as well.


GreetProgram greetProg = new GreetProgram ();
MyInterface myIn = (name) -> {
    return "Welcome " + name;
};
greetProg.greet(myIn);

So this is pretty much the basics about Lamda expression to get a good understanding. How this helps! Thank you.

Comments

Post a Comment

Popular posts from this blog

PHP-SOAP web service with out a WSDL

Boomi Mapping - Removing special chars from an input

How to add Maven dependency from a relative path referencing a local jar