πŸŽ‰ Special Offer !    Code: GET300OFF    Flat β‚Ή300 OFF on every Java Course
Grab Deal πŸš€

Lambda Expressions  


Introduction
  • A Lambda Expression is an anonymous function that has no:
    • Name: It is nameless (anonymous).
    • Access Modifier: Cannot use public, private or protected.
    • Explicitly Declared Return Type: Compiler automatically infers the return type.
  • It was introduced in Java 8 to enable functional programming style.
  • It is used to implement Functional Interfaces without writing a separate class or anonymous inner class.
  • Use of Lambda Expressions:
    1. Reduce Boilerplate Code:
      • Write less code compared to anonymous classes.
    2. Increase Readability:
      • Code becomes clean and more expressive.
    3. Enable Functional Programming:
      • Treat functions as first-class citizens.
    4. Support Stream API:
      • Work with collections using forEach, map, filter, etc.
    5. Simplify Multithreading Code:
      • Pass behavior (e.g., Runnable) directly as a lambda.
    6. Promote Reusability:
      • Easily pass behavior as a parameter to methods.
Syntax:
  • Lambda Expressions are represented using -> (lambda operator).
  • Syntax:
    (parameters) -> expression
    OR
    (parameters) -> { statements }
Program:
  • Program:
    @FunctionalInterface
    interface MyInterface
    {
        void greet(String name);
    }
    
    public class MainApp3
    {
        public static void main(String[] args)
        {
    
            // Using Lambda Expression (Shorthand)
            MyInterface obj = (String name) -> System.out.println("Hello, " + name + "!");
    
            obj.greet("Deepak");  // Calls the lambda implementation
        }
    }

Syntax Shortcuts for Lambda Expression:
  1. We can provide any number of statements (use {} for multiple statements)
    (int no1, int no2) -> {
        int sum = no1 + no2;
        System.out.println("Sum: " + sum);
        return sum;  // βœ… return required when using multiple statements
    }
  2. Providing data type with parameters is optional (compiler infers it). This is called type inference.
    (String name) -> System.out.println("Hello, " + name + "!");    // With type
    (name) -> System.out.println("Hello, " + name + "!");           // Without type (preferred)
  3. If we have only one statement in lambda expression then we can remove curly braces {}; but if you have multiple statements, you must use {}.
    (name) -> System.out.println("Hello, " + name)      // βœ… No braces needed
    (name) -> { System.out.println("Hello, " + name); } // βœ… Braces optional (here unnecessary)
  4. If we have only one parameter, we can remove round braces ()
    name -> System.out.println(name)       // βœ… Parentheses removed
  5. Lambda expression can take any number of parameters
    () -> System.out.println("No parameter")
    (no) -> no * no
    (no1, no2) -> no1 + no2
  6. If lambda is returning a value, we can remove return keyword (single statement)
    (no1, no2) -> { return no1 + no2; }  // βœ… return must be written when using {}
    (no1, no2) -> no1 + no2              // βœ… return is implied