Skip to content
QuizMaker logoQuizMaker
Activity
Java Programming: From Zero to Enterprise
4. Advanced Java & Frameworks
1. Getting Started with Java & the JVM
2. Data Types & Variables
3. Control Flow: Ifs & Loops
4. String Manipulation in Depth
5. Methods (Functions) Architecture
6. Arrays & The Enhanced For Loop
7. User Input via Scanner
8. Mathematical Operations & The Math Class
9. Operators in Depth
10. Block Scope & Variable Lifecycles
11. Introduction to Object-Oriented Programming
12. Classes & Instances Deep Dive
13. Constructors
14. Encapsulation & The 'this' Keyword
15. Inheritance: Extending Functionality
16. Polymorphism & Method Overriding
17. Abstraction & Abstract Classes
18. Interfaces: The Ultimate Contract
19. Packages & Access Modifiers
20. Enums (Enumerations)
21. Exceptions: Handling Runtime Errors
22. The 'throw' and 'throws' keywords
23. Dates, Times, and Formatting
24. Enumerable Data Structures
25. LinkedLists: The Alternative
26. HashMaps: Key-Value Architecture
27. HashSets: The Art of Uniqueness
28. Iterator: Safe Collection Traversal
29. Wrapper Classes & Autoboxing
30. Basic File I/O
31. Generics: Type-Safe Templates
32. Lambda Expressions & Functional Interfaces
33. The Stream API: Functional Data Pipelines
34. Optional: Beating the NullPointerException
35. Multithreading & Concurrency Basics
36. JDBC: Connecting to SQL Databases
37. Annotations & Reflection
38. The JVM Garbage Collector
39. Introduction to Spring Boot
40. Unit Testing with JUnit
41. Java Collections for DSA
CONTENTS

32. Lambda Expressions & Functional Interfaces

Passing behavior as arguments elegantly without massive boilerplate classes.

Java Programming: From Zero to Enterprise
4. Advanced Java & Frameworks
February 22, 2026
63
A

[!NOTE] Introduced in Java 8, Lambda Expressions fundamentally modernized the language. They allow you to pass specific "behavior" (a function) into another function, without having to build a massive, verbose Anonymous Inner Class.

The Boilerplate Problem

Imagine you have a Button object in a UI. You want to tell the button: "When you are clicked, execute this specific block of code."

Historically, you had to physically create a brand new class that implemented the ClickListener interface, write the overriding method, instantiate it, and pass it in.

// 2004 Java (Pre-Java 8)
button.setOnClickListener(new ClickListener() {
    @Override
    public void onClick() {
         System.out.println("Button clicked!");
    }
});

The Lambda Solution

A Lambda expression condenses all of that into a single, elegant arrow function: parameter -> expression.

If the interface only has one abstract method (called a Functional Interface), the Java compiler is smart enough to just inject your arrow function directly into that slot!

// Modern Java 8+ Implementation!
button.setOnClickListener( () -> System.out.println("Button Clicked!") );

Anatomy of a Lambda

Lambdas can take parameters and can have multi-line bodies wrapped in curly braces.

  1. No parameters:

() -> System.out.println("No args");

  1. One parameter (parentheses are optional!):

name -> System.out.println("Hello " + name);

  1. Multiple parameters with a multi-line body (requires { } and 'return'):

(x, y) -> {
    int sum = x + y;
    return sum;
}

Practical Use: Processing ArrayLists

Lambdas shine brightest when interacting with Collections.

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    numbers.add(5);
    numbers.add(9);
    numbers.add(8);
    numbers.add(1);
    
    // Instead of writing a complex Iterator while-loop...
    // We pass a Lambda function that tells '.forEach()' exactly what to do with each element 'n'!
    numbers.forEach( (n) -> { System.out.println(n); } );
  }
}

[!TIP] You cannot use a lambda expression if the target interface has two or more abstract methods. Lambdas ONLY work on Functional Interfaces (like Runnable, Callable, or Comparator) because otherwise the compiler wouldn't know which method you are trying to override!

Lambdas Work Best With Clear Intent

A lambda is a compact way to pass behavior, but compact code is not always clearer. Use lambdas for small, obvious behavior. If the logic grows, move it into a named method.

Comparator Example

List<String> names = new ArrayList<>();
names.add("Ravi");
names.add("Asha");
names.add("Meera");

names.sort((a, b) -> a.compareToIgnoreCase(b));

Method Reference

names.forEach(System.out::println);

A method reference is often cleaner when the lambda only forwards its input to an existing method.

Common Functional Interfaces

InterfaceShapeTypical use
Predicate<T>T to booleanFiltering
Function<T, R>T to RMapping
Consumer<T>T to voidPerforming an action
Supplier<T>No input to TCreating values lazily

Mini Practice

Create a list of marks. Use removeIf with a lambda to remove failed marks, then sort the remaining marks in descending order.

Practice Lab: Sort and Filter With Lambdas

Use small lambda expressions to make collection operations more expressive.

  1. Create an ArrayList<String> of names.
  2. Remove names shorter than four characters using removeIf.
  3. Sort names case-insensitively with a lambda comparator.
  4. Print each name with forEach.
  5. Replace a simple lambda with a method reference where possible.

Goal: Practice lambdas as behavior passed into collection methods.

Revision Checkpoint

  • Lambda: Compact way to pass behavior.
  • Functional interface: Interface with one abstract method.
  • Arrow: Java uses ->.
  • Method reference: Shorter syntax when forwarding to an existing method.
  • Keep it small: Move complex lambda logic into named methods.

Before the quiz: Identify the functional interface behind a lambda example.

Share this article

Share on TwitterShare on LinkedInShare on FacebookShare on WhatsAppShare on Email

Test your knowledge

Take a quick quiz based on this chapter.

mediumJava
Quiz: Lambdas
13 questions5 min
Lesson 2 of 11 in 4. Advanced Java & Frameworks
Previous in 4. Advanced Java & Frameworks
31. Generics: Type-Safe Templates
Next in 4. Advanced Java & Frameworks
33. The Stream API: Functional Data Pipelines
Back to Java Programming: From Zero to Enterprise
Back to moduleCategories