Top 5 Use Case of Lambda Expressions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

TOP USE CASE OF LAMBDA EXPRESSION

LAMBDA
EXPRESSIONS
in
Java
EVERY DEVELOPER
SHOULD KNOW THIS

Sanjay Yadav
Software Developer
1. Sorting Collections
Lambda expressions are useful for defining custom
comparison logic when sorting collections. This is
particularly handy when working with complex objects or
sorting based on specific attributes

List<String> names = Arrays.asList("Sanjay", "Ram", "Lalit");


names.sort((name1, name2) -> name1.compareTo(name2));

Sanjay Yadav
Software Developer
1
2.Thread Management
Lambda expressions can be used to define the behavior of
threads, making code more concise and readable,
especially when working with multithreading.

Thread
optionalValue != null) thread with
{ // Do something = new Thread(() -> {
// Thread
e value } else { // Handle behavior
the case where the defined here
value is null }
System.out.println("Thread is running"); });

Sanjay Yadav
Software Developer 2
3. Stream Processing
1. Lambda expressions are extensively used with the Stream
API for processing collections of data in a functional style.

2.They allow for concise and expressive transformations and


operations on streams of element

List<Integer>
optionalValue numbers
!= null) { // Do something = Arrays.asList(1,
with 2, 3, 4, 5);
e value } else { // Handle int sum
the case = numbers.stream()
where the
value is null }
.filter(num -> num % 2 == 0)
.mapToInt(num -> num * num)
.sum();

Sanjay Yadav
Software Developer 3
4.Callback Functions
Lambda expressions can be used to define callback
functions or listeners, allowing for clean and concise code
when working with asynchronous tasks or event-driven
architectures.

public interface Callback {


void onComplete(String result);
}
optionalValue != null) { // Do something with
public
e value } else { // Handle the case class
where the AsyncTask {
value ispublic
null } void execute(Callback callback) {
// Asynchronous task execution
String result = "Task completed";
callback.onComplete(result);
}
}

// Usage
AsyncTask task = new AsyncTask();
task.execute(result -> System.out.println("Callback: " +
result));

Sanjay Yadav
Software Developer 4
5.Dependency Injection:
Lambda expressions can be employed for defining lightweight
anonymous implementations of functional interfaces during
dependency injection, especially in frameworks like Spring.

@Autowired
private SomeService someService;
optionalValue != null) { // Do something with
e value } else { // Handle the case where the
value is null }someService.doSomething(() ->
{
// Logic here

});

Sanjay Yadav
Software Developer 5
6.Exception Handling:
Lambda expressions can be used to define exception-handling
logic, especially when working with functional interfaces that
throw checked exceptions.

try {

Files.lines(Paths.get("file.txt"))
optionalValue != null) { // Do something with
.forEach(line ->
e value } else { // Handle the case where the
value is null }
System.out.println(line));

} catch (IOException e) {

System.err.println("Error reading file: " +


e.getMessage());
}

Sanjay Yadav
Software Developer 5
Follow Me for This Type of
Content

Sanjay Yadav
Software Developer

You might also like