Java 8 – What is Lambda Expression? Why it is used for?

Lambda Expressions were added in Java 8. In Simple words,  We can think of lambda expression as a function without a name and with or without a parameter.

				
					public void MyFunction(int param1, int param2){
}
				
			

The Above MyFunction() Method can be written in Lambda Expression as follows:

				
					(param1,param2) -> { } //Example 1
				
			
				
					() -> { }  //Example 2 Without Parameter
				
			

But Above Definition is not sufficient to explain Lambda expression. Let’s look into another definition of a lambda expression. “A Lambda expression is an implementation of functional interface”.

Table of Contents

What is Functional Interface?

Functional Interface is just like a normal interface but the difference is normal interface can contain more than one abstract method. On other hand, Function Interface will have only one abstract method.

We can create a functional interface like a normal interface. @FunctionalInterface annotation is optional, you can use this annotation to avoid a human error like adding more than one method so that compiler can catch it. 

Example for Functional Interface:

				
					@FunctionalInterface   //It is optional
interface PrintMessage{
    public void message(String text);
}
				
			

Before Java 8, If we want to implement the interface methods we require the implementation class which implements the interface and provides the implementation.

Implementation of Interface Method without Lambda:

				
					public class PrintMessageImpl implements PrintMessage{
    
    @Override
    public void message(String text){
      System.out.println("Message: " + text);
    }
}
				
			

Let’s Come to our Definition: “A Lambda expression is an implementation of functional interface”.

We can implement the functional interface method using lambda expression as follows:

				
					public class LambdaExample{
    public static void main(String[] args){
        //Implementation using lambda
        PrintMessage msg = (a)->{
          System.out.println("Message: " + a);
        };
        msg.message("Hello World");
    }
}
				
			

Now we understood that how to implement a functional interface method using a lambda expression.

Lamba Expression Without Parameters

We use lambda expression without any parameter as well.

				
					@FunctionalInterface   //It is optional
interface PrintMessage{
    public String message();
}
				
			
				
					public class LambdaExample{
    public static void main(String[] args){
        //Implementation using lambda
        PrintMessage msg = ()->{
          return "Hello world";
        };
        msg.message();
    }
}
				
			

Why do we use lambda expression?

To answer this question, Let’s look into the third definition. “Lambda expression can be used behavior as a parameter (Arguments)”. 

Wouldn’t be amazing if you can send an implementation to a method parameter?  Yes, this can be achieved with a lambda expression.

Look into the below functional interface with has the method message(). Now we will send the message() implementation to another method as a parameter.

				
					@FunctionalInterface   //It is optional
interface PrintMessage{
    public String message();
}
				
			

Method Signature:

				
					public void operation(int a, int b, PrintMessage msg);
				
			

While you call the operation() method you have to provide the implementation for PrintMessage.

				
					public class LambdaExample{
    public static void main(String[] args){
       //calling 
       operation(5,6,()->{
            return "Addition ";
        }
       );
    }
    public static void operation(int a, int b, PrintMessage msg){
     System.out.println(msg.message() + (a+b));
    }
}
				
			

Output:

				
					Addition 11
				
			

Conclusion:

Lambda expression is important feature of Java 8. This version of java is a Major version and you must hands on all the features of Java 8. Lambda expression provides a clean and concise way to represent one method using an expression. This is also used in Collection Framework. it is very useful to iterate, filter etc.

Did you Know?

  • When you use lambda express and implements it. it doesn’t create any .class file.
  • Java 8 Stream APIs uses lambda expressions.
  • Built-in Functional Interfaces:
    •  Function Interface
    • Supplier Interface
    • Consumer Interface
    • Predicate Interface
Java 8 Stream APIs

Java 8: Introduction to Streams API

Java 8 got a brand new API called streams. it makes every developer’s life easy. Stream APIs are used to process the group of data. mostly streams are applied on collection objects but we can use stream with any group

Read More »