Java 8 Features: Lambda Expression

Java 8 Features: Lambda Expression

What is a Lambda Expression?

In simple terms, a lambda expression is a way to implement a method using a short, readable expression. It’s used with functional interfaces, which means interfaces with only one abstract method. Instead of writing a full class or method, we can use a lambda expression to implement the functional interface directly.

Why Use Lambda Expressions?

  • Less code: Lambda expressions reduce the amount of boilerplate code needed to implement interfaces.
  • Functional Interface Implementation: Lambda expressions simplify the way we implement functional interfaces.
  • Efficient Data Processing: It helps in iterating, filtering, and extracting data from collections using a concise syntax.

Lambda Expression Syntax:

(argument-list) -> {body}        

  1. Argument-list: This can be empty or have one or more arguments.
  2. Arrow-token: The arrow -> separates the arguments and the body of the expression.
  3. Body: The body contains the statements or expressions to be executed.

Lambda Expression Examples:

  • No Parameters:

() -> { 
    // No parameter lambda body 
}        

  • One Parameter:

(p1) -> { 
    // Single parameter lambda body 
}        

  • Two Parameters:

(p1, p2) -> { 
    // Multiple parameter lambda body 
}        

Without Lambda Expression:

Here’s an example of how we used to implement functional interfaces before lambda expressions were introduced:

interface Drawable {  
    public void draw();  
}  
public class LambdaExpressionExample {  
    public static void main(String[] args) {  
        int width = 10;  
        
        // Without lambda, Drawable implementation using anonymous class
        Drawable d = new Drawable() {  
            public void draw() {
                System.out.println("Drawing " + width);
            }
        };  
        d.draw();  
    }  
}        

With Lambda Expression:

Now, here’s the same functionality, but using a lambda expression. Notice how much cleaner and shorter the code becomes:

interface Drawable {  
    public void draw();  
}  
public class LambdaExpressionExample {  
    public static void main(String[] args) {  
        int width = 10;  
        
        // Lambda expression to implement Drawable
        Drawable d = () -> {  
            System.out.println("Drawing " + width);  
        };  
        d.draw();  
    }  
}        

Benefits of Lambda Expressions:

  • Simplified Code: Less code means fewer chances for errors and easier maintenance.
  • Functional Programming: Lambda expressions allow Java to adopt a more functional style of programming.
  • Better Readability: Lambda expressions make the code cleaner and easier to understand.

Conclusion:

Lambda expressions have transformed the way we write Java code by reducing boilerplate and making functional programming easier. This feature plays a key role in modern Java development, making our code more expressive, readable, and maintainable.

Vedant Joshi kindly send me a connection request or message me directly to discuss more about your job search process 😊😊

Like
Reply

To view or add a comment, sign in

More articles by Vedant Joshi

  • Java 8 Features: Stream API

    What is the Stream API? The Stream API in Java 8 provides a way to process collections of objects. A stream is a…

    4 Comments
  • Java 8 Features: Functional Interfaces

    As Java developers, we are constantly exploring new features that make our code simpler and more efficient. One of the…

  • Java 8 Features: Default Methods in Interfaces

    As Java developers, we are constantly adapting to new features and advancements that make our code more efficient…

Explore content categories