Lambda Expressions in Java
0 246
🔍 Introduction to Lambda Expressions in Java
Lambda Expressions in Java were introduced in Java 8 to bring functional programming concepts into the language. They allow us to write cleaner, more concise, and more readable code by treating functions as first-class citizens.
Lambda expressions are primarily used to implement interfaces with a single abstract method, also known as functional interfaces.
🧠 Why Use Lambda Expressions?
Traditionally, to use an interface like Runnable
or Comparator
, we had to create anonymous inner classes. This added unnecessary boilerplate to our code.
Lambda expressions simplify this by reducing syntax, which leads to more maintainable and readable code.
🔧 Syntax of Lambda Expressions
The basic syntax of a lambda expression is:
(parameter1, parameter2, ...) -> { // body }
For example:
Runnable r = () -> System.out.println("Running a thread");
This code replaces the need for an anonymous inner class to define a Runnable
object.
📘 Examples of Lambda Expressions
1. Runnable using Lambda
Runnable task = () -> {
System.out.println("Task is executing...");
};
new Thread(task).start();
2. Comparator using Lambda
List<String> names = Arrays.asList("Java", "Python", "C++", "Kotlin");
Collections.sort(names, (a, b) -> a.compareTo(b));
System.out.println(names);
3. Iterating with forEach
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.forEach(n -> System.out.println(n));
🎯 Functional Interfaces
A lambda expression can only be used with a functional interface.
Some commonly used functional interfaces are:
Runnable
Callable
Comparator
Consumer
(from java.util.function)Predicate
(from java.util.function)
🧩 Benefits of using Lambda Expressions
- Concise Code: Reduces verbosity.
- Improved Readability: Logic is expressed clearly.
- Easy Integration: Works well with Java Streams API and collections.
- Functional Programming Style: Encourages immutability and statelessness.
⚠️ Things to Keep in Mind
- Lambdas can only be used with functional interfaces.
- A lambda does not have its own scope like an anonymous class.
- Variable capture rules apply – lambdas can only access final or effectively final variables.
🧪 Real-World Use Case
Suppose you want to filter a list of strings that start with "J":
List<String> list = Arrays.asList("Java", "JavaScript", "Python", "Ruby");
list.stream()
.filter(s -> s.startsWith("J"))
.forEach(System.out::println);
Output:
Java
JavaScript
📝 Conclusion
Lambda Expressions in Java have revolutionized the way developers write code by introducing a more functional and expressive syntax. They reduce the verbosity of anonymous classes and integrate seamlessly with Java’s Stream API.
If you're aiming to write modern, clean, and efficient Java code, mastering lambdas is a must.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments