codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

60 / 65 topics
56Java Wrapper Classes57Java Generics58Java RegEx59Java Threads60Java Lambda
Tutorials/Java Programming/Java Lambda
☕Java Programming

Java Lambda

Updated 2026-05-12
20 min read

Java Lambda

Lambda expressions are a powerful feature introduced in Java 8 that allow you to write more concise and readable code. They provide a way to represent one method interface using an expression. This tutorial will guide you through the basics of lambda expressions, how they relate to functional interfaces, and how to pass lambdas as parameters.

Introduction

Lambda expressions are particularly useful when working with collections and streams in Java. They enable you to write more expressive and functional-style code. Understanding lambda expressions is crucial for leveraging modern Java features effectively.

What Are Lambda Expressions?

A lambda expression is a short block of code which takes in parameters and returns a value. In Java, lambda expressions are used to implement single-method interfaces (functional interfaces) in a concise way.

Syntax

The general syntax of a lambda expression is:

Java
1

or

Java
1(parameters) -> { statements; }
  • Parameters: The input parameters that the lambda takes.
  • Arrow (->): Separates the parameters from the body of the lambda.
  • Expression/Statements: The code executed by the lambda.

Example 1: Simple Lambda Expression

Let's start with a simple example where we define a lambda expression to add two numbers. We'll use the IntBinaryOperator functional interface, which represents an operation accepting two operands of type int.

SimpleLambda.java
1import java.util.function.IntBinaryOperator;
2
3public class SimpleLambda {
4 public static void main(String[] args) {
5 // Define a lambda expression for adding two numbers
6 IntBinaryOperator add = (a, b) -> a + b;
7
8 // Call the lambda and print the result
9 int result = add.applyAsInt(10, 20);
10 System.out.println("Result: " + result);
11 }
12}
Output
Result: 30

In this example:

  • We define a lambda expression (a, b) -> a + b that takes two integers and returns their sum.
  • We assign this lambda to an IntBinaryOperator variable named add.
  • We call the applyAsInt method on add with arguments 10 and 20, which executes the lambda expression and prints the result.

Functional Interfaces

A functional interface is an interface that has exactly one abstract method. Lambda expressions can be used to implement these interfaces concisely. Java provides several built-in functional interfaces in the java.util.function package, such as:

  • Predicate<T>: Represents a predicate (boolean-valued function) of one argument.
  • Function<T, R>: Represents a function that accepts one argument and produces a result.
  • Consumer<T>: Represents an operation that accepts a single input argument and returns no result.
  • Supplier<T>: Represents a supplier of results.

Example 2: Using Functional Interfaces

Let's use the Predicate functional interface to check if a number is even.

FunctionalInterfaces.java
1import java.util.function.Predicate;
2
3public class FunctionalInterfaces {
4 public static void main(String[] args) {
5 // Define a lambda expression for checking if a number is even
6 Predicate<Integer> isEven = n -> n % 2 == 0;
7
8 // Test the predicate with different numbers
9 System.out.println("Is 4 even? " + isEven.test(4));
10 System.out.println("Is 7 even? " + isEven.test(7));
11 }
12}
Output
Is 4 even? true
Is 7 even? false

In this example:

  • We define a lambda expression n -> n % 2 == 0 that checks if a number is even.
  • We assign this lambda to a Predicate&lt;Integer&gt; variable named isEven.
  • We use the test method of isEven to check if numbers 4 and 7 are even.

Passing Lambdas as Parameters

Lambdas can be passed as parameters to methods, allowing for more flexible and reusable code. This is particularly useful when working with collections and streams.

Example 3: Passing a Lambda as a Parameter

Let's create a method that takes a lambda expression as a parameter and uses it to filter a list of numbers.

PassingLambdas.java
1import java.util.Arrays;
2import java.util.List;
3import java.util.function.Predicate;
4
5public class PassingLambdas {
6 public static void main(String[] args) {
7 List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
8
9 // Define a lambda expression for checking if a number is even
10 Predicate<Integer> isEven = n -> n % 2 == 0;
11
12 // Print even numbers using the lambda
13 printNumbers(numbers, isEven);
14 }
15
16 public static void printNumbers(List<Integer> numbers, Predicate<Integer> condition) {
17 for (Integer number : numbers) {
18 if (condition.test(number)) {
19 System.out.println(number);
20 }
21 }
22 }
23}
Output
2
4
6

In this example:

  • We define a lambda expression n -> n % 2 == 0 that checks if a number is even.
  • We pass this lambda to the printNumbers method, which filters and prints numbers based on the provided condition.

Practical Example

Let's create a practical example where we use lambdas to sort a list of strings by their length.

PracticalExample.java
1import java.util.Arrays;
2import java.util.List;
3import java.util.function.Comparator;
4
5public class PracticalExample {
6 public static void main(String[] args) {
7 List<String> words = Arrays.asList("apple", "banana", "pear", "grape");
8
9 // Define a lambda expression for comparing strings by length
10 Comparator<String> compareByLength = (s1, s2) -> Integer.compare(s1.length(), s2.length());
11
12 // Sort the list using the lambda
13 words.sort(compareByLength);
14
15 // Print the sorted list
16 System.out.println(words);
17 }
18}
Output
[pear, apple, grape, banana]

In this example:

  • We define a lambda expression (s1, s2) -> Integer.compare(s1.length(), s2.length()) that compares two strings by their length.
  • We use this lambda to sort a list of words and print the sorted list.

Summary

ConceptDescription
Lambda ExpressionA short block of code representing a single method interface.
Functional InterfaceAn interface with exactly one abstract method that can be implemented by a lambda.
Passing LambdasUsing lambdas as parameters to methods for more flexible and reusable code.

What's Next?

In the next section, we will explore Java keywords in detail. Understanding keywords is essential for writing effective and idiomatic Java code. Make sure you have a solid grasp of lambda expressions before moving on to learn about other important Java features.


This tutorial provides a comprehensive introduction to lambda expressions in Java, including functional interfaces and passing lambdas as parameters. By the end of this section, you should be able to write concise and expressive code using lambda expressions in your Java applications.


PreviousJava ThreadsNext Java Keywords

Recommended Gear

Java ThreadsJava Keywords