Java

Module 2: Functional Interfaces & Lambda Expressions

By Utility Zone · 2026-01-27T14:37:09.347716

Phase

Phase 1 – Foundation

Target Audience

  • Java developers moving from Java 7 to Java 8
  • Developers who want to write clean, modern Java code
  • Beginners learning Java 8 fundamentals

1. Objective of This Module

By the end of this module, you will:

  • Understand what Functional Interfaces are
  • Learn why Lambdas exist
  • Write Lambda Expressions confidently
  • Use built-in functional interfaces in real scenarios

2. What Is a Functional Interface?

A Functional Interface is an interface that:

  • Has exactly one abstract method
  • Can have default and static methods
  • Acts as the foundation for Lambda expressions

Example

@FunctionalInterface
public interface Calculator {
    int add(int a, int b);
}

This interface can be implemented using a Lambda.


3. @FunctionalInterface Annotation

  • Optional but recommended
  • Helps the compiler enforce the rule of one abstract method
  • Improves readability and intent
@FunctionalInterface
interface Printer {
    void print(String message);
}

4. Why Lambda Expressions Were Introduced

Before Java 8, behavior was passed using:

  • Anonymous inner classes
  • Verbose syntax

Java 7 Style

Runnable r = new Runnable() {
    @Override
    public void run() {
        System.out.println("Running...");
    }
};

Java 8 Style

Runnable r = () -> System.out.println("Running...");

✔ Less code
✔ More readable
✔ Clear intent


5. Lambda Expression Syntax

Basic Syntax

(parameters) -> expression

Examples

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

name -> name.toUpperCase();

(a, b) -> a + b;

6. Built-in Functional Interfaces

Java 8 provides commonly used functional interfaces in java.util.function.

Predicate<T>

  • Takes input
  • Returns boolean
Predicate<Integer> isEven = n -> n % 2 == 0;

Function<T, R>

  • Takes input
  • Returns output
Function<String, Integer> length = s -> s.length();

Consumer<T>

  • Takes input
  • Returns nothing
Consumer<String> printer = s -> System.out.println(s);

Supplier<T>

  • Takes no input
  • Returns output
Supplier<Double> random = () -> Math.random();

7. Lambda vs Anonymous Class

FeatureAnonymous ClassLambda
BoilerplateHighLow
ReadabilityLowHigh
this referenceRefers to classRefers to enclosing scope
ReusabilityPoorBetter

8. Effectively Final Variables

  • Lambdas can access local variables
  • Variables must be effectively final
int x = 10;
Runnable r = () -> System.out.println(x);

❌ This will fail:

x++;

9. Real-World Use Cases

  • Sorting collections
  • Validation logic
  • Callbacks
  • Event handling
  • Passing behavior to methods

10. Hands-On Exercises

Exercise 1

Create a functional interface for:

  • User validation

Exercise 2

Use Predicate to:

  • Filter even numbers from a list

Exercise 3

Use Consumer for:

  • Logging messages

11. Common Mistakes

  • Creating unnecessary functional interfaces
  • Overusing lambdas for complex logic
  • Ignoring readability

12. Summary

  • Functional Interfaces enable Lambdas
  • Lambdas reduce boilerplate
  • Built-in interfaces cover most use cases

13. What’s Next?

➡️ Module 3: Method References & Code Readability

Keep going 🚀