Java 8 Lambda Expressions

Java 8 Lambda Expressions – Complete Guide with Examples

In this post, we will explore one of the most important features introduced in Java 8Lambda Expressions. With practical examples, you’ll learn how they work, why they are useful, and how they simplify coding in Java.

1. What Are Lambda Expressions?

A lambda expression is essentially a function without a name. It can even be passed as a parameter to methods. Lambda expressions make it easier to implement functional programming concepts in Java and significantly reduce boilerplate code.

👉 Main purpose: Provide implementation for functional interfaces (interfaces with exactly one abstract method).

Syntax of a Lambda Expression:


Parameters -> Body (expression/code)

A lambda expression has three main parts:

  • ParametersZero or more parameters inside parentheses.
  • Arrow token (->)Separates parameters from the body.
  • Body Defines the logic; can be a single expression or multiple statements.

Syntax :

Multiple prameter 
(int a , int b) -> a+b

2. Lambda Expressions and Functional Interfaces

Lambda expressions work with functional interfaces—interfaces containing a single abstract method (SAM).

Examples of functional interfaces in Java:

  • Runnable
  • Callable
  • Comparator
  • FileFilter
  • Predicate, Function, Consumer, Supplier (from java.util.function package)

Java 8 also provides the @FunctionalInterface annotation to explicitly declare an interface as functional.

3. Benefits of Using Lambda Expressions

  • ConcisenessEliminates boilerplate code of anonymous inner classes.
  • Improved ReadabilityMakes code easier to follow.
  • Functional Programming StyleEnables functional-style constructs.
  • FlexibilityCan be used anywhere a functional interface is expected.
  • Seamless with Streams APIPerfectly integrates with Java Streams for powerful data processing.

4. Use Cases of Lambda Expressions

  • Implementing functional interfaces (Runnable, Comparator, etc.).
  • Simplifying collection operations (forEach, filtering, mapping, reducing).
  • Enhancing multithreading with concise syntax.
  • Cleaner and more expressive event handling.

5. Examples of Java Lambda Expressions

5.1 Simple Lambda Example

interface Shape {  
void display(); } public class LambdaExpressionExample { public static void main(String[] args) { int radius = 5; // Lambda expression implementing Shape interface Shape circle = () -> System.out.println("Circle radius = " + radius); // Call the method circle.display(); } }

✅ Output:

Circle radius = 5


5.1 Simple Lambda Example

interface Shape {  
void display(); } public class LambdaExpressionExample { public static void main(String[] args) { int radius = 5; // Lambda expression implementing Shape interface Shape circle = () -> System.out.println("Circle radius = " + radius); // Call the method circle.display(); } }

✅ Output:

Circle radius = 5

5.2 No Parameter Example

interface Message {
    String getMessage();
}

public class NoParameterExample {
    public static void main(String[] args) {
        Message message = () -> "Learning Lambda Expressions is fun!";
        System.out.println(message.getMessage());
    }
}

✅ Output:

Learning Lambda Expressions is fun!

5.3 Single Parameter Example

interface Message {
    void getMessage(String msg);
}

public class SingleParameterExample {
    public static void main(String[] args) {
        Message message = msg -> System.out.println(msg);
        message.getMessage("Lambda with Single Parameter");
    }
}

✅ Output:

Lambda with Single Parameter

5.4 Multiple Parameters Example

interface Area {  
    void area(int width, int height); 
}  

public class MultipleParametersExample {  
    public static void main(String[] args) {  
        int width = 10;  
        int height = 10;
        // Lambda expression with multiple parameter
        Area rectangle = (width, height) -> width * height;  
        System.out.println("Rectangle Area = " + rectangle.area(10, 20));  
    }  
} 

✅ Output:

Rectangle Area = 200

No comments:

Post a Comment

Java Development Kit (JDK) and Java Runtime Environment (JRE)

                  Java Development Kit (JDK) and Java Runtime Environment (JRE)  To download and install the Java Development Kit (  JDK ) ...