Java 8 Lambda Expressions – Complete Guide with Examples
In this post, we will explore one of the most important features introduced in Java 8 – Lambda 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:
- Parameters → Zero or more parameters inside parentheses.
- Arrow token (->) → Separates parameters from the body.
- Body → Defines the logic; can be a single expression or multiple statements.
(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(fromjava.util.functionpackage)
Java 8 also provides the @FunctionalInterface annotation to explicitly declare an interface as functional.
3. Benefits of Using Lambda Expressions
- Conciseness → Eliminates boilerplate code of anonymous inner classes.
- Improved Readability → Makes code easier to follow.
- Functional Programming Style → Enables functional-style constructs.
- Flexibility → Can be used anywhere a functional interface is expected.
- Seamless with Streams API → Perfectly 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
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
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
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!
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
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));
}
}
No comments:
Post a Comment