Functional Programming in Java: A Deep Dive into Lambdas and Streams
you won’t just memorize APIs — you’ll understand why they exist.
If you’ve been preparing for Java backend interviews, you’ve probably noticed one thing: Lambdas and the Stream API appear everywhere.
Whether you’re working with collections, processing data, or writing modern Java applications, understanding these concepts isn’t optional anymore.
But many tutorials jump straight into filter(), map(), and collect() without explaining why Java introduced Lambdas in the first place.
That’s the difference between answering interview questions confidently and simply recalling syntax.
In this article, we’ll build that foundation first and then progressively move through the Stream API until you’re comfortable with both the concepts and the interview questions.
Before Java 8: The Problem
Java has always been an Object-Oriented language.
Passing objects around was easy.
Passing behavior (logic) was not.
Imagine you have a list of employees.
Today you need to filter employees by salary.
Tomorrow by age.
Next week by department.
The iteration logic never changes — only the filtering condition does.
Before Java 8, every new behavior required another anonymous class.
new Filter() {
@Override
public boolean test(Employee e) {
return e.getSalary() > 50000;
}
}The actual business logic is just one line:
e.getSalary() > 50000Everything else is ceremony.
As projects grew, this resulted in:
- Lots of boilerplate code
- Poor readability
- Difficult maintenance
- No clean way to pass behavior as a parameter
Java needed a better solution.
Enter Functional Programming
Functional Programming (FP) is a programming style where behavior can be treated like data.
Instead of writing multiple methods:
filterBySalary()
filterByAge()
filterByDepartment()You write one reusable method:
filter(list, condition)The algorithm stays the same.
Only the condition changes.
Unlike languages such as Haskell or Scala, Java did not become a purely functional language.
Instead, Java 8 added functional programming features while keeping its object-oriented foundation.
Interview Tip: Java supports functional programming but is not a pure functional programming language.
Functional Interfaces: The Foundation of Lambdas
A Lambda can only work with a Functional Interface.
A Functional Interface contains exactly one abstract method (SAM — Single Abstract Method).
Example:
@FunctionalInterface
interface Calculator {
int add(int a, int b);
}This is valid because there’s only one abstract method.
This isn’t:
interface Service {
void save();
void delete();
}Since there are two abstract methods, Java wouldn’t know which one the Lambda should implement.
Why @FunctionalInterface?
The annotation is optional but highly recommended because it provides compile-time validation.
It prevents someone from accidentally adding another abstract method later.
A Functional Interface can still contain:
- Default methods
- Static methods
- Methods inherited from
Object
Only abstract methods count toward the SAM rule.
Common Functional Interfaces Every Java Developer Should Know
You’ll encounter these repeatedly while working with Streams.
Lambda Expressions
A Lambda Expression is simply a concise implementation of a Functional Interface.
General syntax:
(parameters) -> expressionInstead of writing:
Calculator calculator = new Calculator() {
@Override
public int add(int a, int b) {
return a + b;
}
};You can write:
Calculator calculator = (a, b) -> a + b;The unnecessary syntax disappears.
Only the business logic remains.
Type Inference Makes Lambdas Cleaner
Java already knows the parameter types from the Functional Interface.
Instead of:
(int a, int b) -> a + bWrite:
(a, b) -> a + bCleaner.
More readable.
Preferred in production code.
Lambda Syntax Variations
No Parameters
() -> System.out.println("Hello")One Parameter
name -> System.out.println(name)Multiple Parameters
(a, b) -> a + bMultiple Statements
(a, b) -> {
int sum = a + b;
return sum;
}Variable Capture and Effectively Final
Lambdas can access variables from the surrounding scope.
int bonus = 1000;
employees.stream()
.filter(e -> e.getSalary() > bonus);However, the captured variable must be effectively final.
This works:
int bonus = 1000;This doesn’t:
bonus++;Java captures the value of the local variable, not the variable itself. Allowing modifications afterward could lead to confusing and unsafe behavior.
Method References
Sometimes a Lambda simply calls an existing method.
Instead of:
name -> System.out.println(name)You can write:
System.out::printlnThis is called a Method Reference.
There are four common types:
Static Method
Integer::parseInt
Math::absInstance Method of a Particular Object
printer::printInstance Method of an Arbitrary Object
String::length
Employee::getName