---
title: "What are lambda expressions in Java, and how are they used?"  
description: "What are lambda expressions in Java, and how are they used?"  
author: "Revati S Misra"  
published: 2024-07-19  
updated: 2024-07-19  
canonical: https://www.mindstick.com/forum/160966/what-are-lambda-expressions-in-java-and-how-are-they-used  
category: "java"  
tags: ["java", "programming language", "lambda expression"]  
reading_time: 3 minutes  

---

# What are lambda expressions in Java, and how are they used?

What are lambda expressions in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming), and how are they used?

## Replies

### Reply by Ashutosh Patel

#### Lambda Expressions in Java

Lambda expressions in Java are a shorthand way of representing anonymous functions: functions without a name that can be executed later by passing them around. They are essentially a way of defining a method without formally declaring it in a class.

## The syntax for a lambda expression

The simple lambda expressions contain a single parameter and expression,

```java
parameter -> expression
```

If you need to use more than one parameter, wrap them into a parentheses ().

```java
(parameter1, parameter2) -> expression
```

Here is a simple example where a **lambda expression** is used to define a function that calculates the square of a number,

## Function without lambda expression

```java
public class Main {
   public static void main(String[] args) {

       System.out.println(myFuntion(5, 4));
   }
   public static int myFuntion(int a, int b) {
       return a * b;
   };
}
```

## Function with lambda expression

```java
import java.util.ArrayList;
class Program {
   public static void main(String args[])
   {
       // Creating an ArrayList with elements {1, 2, 3, 4, 5, 6, 7}
       ArrayList<Integer> arrList = new ArrayList<Integer>();
       arrList.add(1);
       arrList.add(2);
       arrList.add(3);
       arrList.add(4);
       arrList.add(5);
       arrList.add(6);
       arrList.add(7);
       // Using lambda expression to print all elements
       System.out.println("All elements");
       arrList.forEach(n -> System.out.print(n+ "\t"));
       // Using lambda expression to print even elements
       System.out.println("\nList of Even Numbers");
       arrList.forEach(n -> {
           if (n % 2 == 0)
               System.out.print(n+ "\t");
       });

       // Using lambda expression to print odd elements
       System.out.println("\nList of Odd Numbers");
       arrList.forEach(n -> {
            if(n%2 != 0)
                System.out.print(n+ "\t");
       });
   }
}
```

## Output-

```plaintext
All elements
1	2	3	4	5	6	7
List of Even Numbers
2	4	6
List of Odd Numbers
1	3	5	7
```

#### Usage

Lambda expressions are mainly used in functional networks, which are exactly one-way abstract interfaces. They provide a concise way to implement a method defined by a user interface.

For example, in Java, the `java.util.function` package contains many function interfaces such as `Predicate`, `Consumer`, and `Function`, which can be effectively implemented in lambda expressions.

#### Benefits

**Conciseness-** Lambda expressions reduce the amount of boilerplate code that comes with anonymous classes.\
**Readability-** The code is made readable by focusing on what needs to be done rather than how.\
**Functional Programming-** Lambda expressions enable a highly efficient functional form in Java, facilitating tasks such as mapping, filtering, and reducing collections.

#### \
Limitations

There are some restrictions on lambda expressions:

Can only be used in functional interfaces.\
Cannot have `return` statements without curly braces {} around the body.\
Cannot contain `void` as a return type (use `Consumer` for the void method).\

#### When to Use

Lambda expressions are especially useful in situations where you need to pass a statement (to some extent) as an argument to a method or do it asynchronously. It is widely used in streams, threads, event handling, and other functional programming constructs.

**Also, Read:** [What are lambda expressions, and how are they used in LINQ queries?](https://www.mindstick.com/forum/159874/what-are-lambda-expressions-and-how-are-they-used-in-linq-queries)


---

Original Source: https://www.mindstick.com/forum/160966/what-are-lambda-expressions-in-java-and-how-are-they-used

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
