---
title: "Where and when to use Lambda in coding?"  
description: "Where and when to use Lambda in coding?"  
author: "Steilla Mitchel"  
published: 2023-08-17  
updated: 2023-08-18  
canonical: https://www.mindstick.com/forum/159555/where-and-when-to-use-lambda-in-coding  
category: "c#"  
tags: ["java", "programming language", "lambda expression"]  
reading_time: 2 minutes  

---

# Where and when to use Lambda in coding?

Where and when to use [Lambda](https://www.mindstick.com/blog/181/lambda-expression-in-c-sharp) in [coding](https://www.mindstick.com/articles/12290/teaching-coding-from-the-metal-up-or-from-the-glass-back)?

## Replies

### Reply by Aryan Kumar

Lambda expressions can be used in a variety of places in coding, including:

- **Wherever you would use a regular function:** Lambda expressions can be used wherever you would use a regular function. For example, you can use a lambda expression as the argument to a method, or you can use it to define a new function.
- **In collection operations:** Lambda expressions can be used in collection operations, such as `filter()`, `map()`, and `reduce()`. These operations allow you to perform complex operations on collections without having to write a lot of code.
- **In streams:** Lambda expressions can be used in streams, which are a new feature in Java 8. Streams allow you to process data in a declarative way, without having to worry about the details of how the data is processed.
- **In functional programming:** Lambda expressions are a key feature of functional programming, which is a programming paradigm that emphasizes the use of functions. Functional programming can help you write code that is more concise, readable, and maintainable.

Here are some examples of where and when to use lambda in coding:

- You can use a lambda expression as the argument to the `filter()` method to filter a collection of objects. For example, the following code filters a list of people to only include people who are over 18 years old:

```plaintext
List<Person> people = new List<Person>();
people.add(new Person("John", 20));
people.add(new Person("Jane", 30));
people.add(new Person("Peter", 15));

List<Person> adults = people.stream().filter(p -> p.getAge() > 18).collect(Collectors.toList());
```

- You can use a lambda expression as the argument to the `map()` method to transform a collection of objects. For example, the following code maps a list of people to their names:

```plaintext
List<Person> people = new List<Person>();
people.add(new Person("John", 20));
people.add(new Person("Jane", 30));
people.add(new Person("Peter", 15));

List<String> names = people.stream().map(p -> p.getName()).collect(Collectors.toList());
```

- You can use a lambda expression as the argument to the `reduce()` method to reduce a collection of objects to a single value. For example, the following code finds the sum of the ages of all the people in a list:

```plaintext
List<Person> people = new List<Person>();
people.add(new Person("John", 20));
people.add(new Person("Jane", 30));
people.add(new Person("Peter", 15));

int sumOfAges = people.stream().reduce(0, (a, b) -> a + b);
```


---

Original Source: https://www.mindstick.com/forum/159555/where-and-when-to-use-lambda-in-coding

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
