A programming paradigm where functions are first-class citizens - treated like values.
In Java, functional programming was introduced with Java 8, enabling:
- more concise, expressive code,
- declarative data processing (Streams),
- use of lambda expressions, functional interfaces and method references.
Functional vs Imperative Style
Imperative (Old School) | Functional (Modern Java) |
---|---|
βHow to do itβ - step-by-step | βWhat to doβ - describe the operation |
Uses loops, conditionals | Uses functions like map(), filter() |
Verbose | Concise, expressive |
// Imperative
List<String> result = new ArrayList<>();
for (String name : names) {
if (name.startsWith("A")) result.add(name);
}
// Functional
List<String> result = names.stream()
.filter(n -> n.startsWith("A"))
.collect(Collectors.toList());
Key Functional Concepts in Java
Concept | Purpose |
---|---|
Lambda Expressions | Inline functions (no boilerplate) |
Functional Interfaces | Interfaces with a single abstract method |
Streams API | Functional-style data processing |
Method References | Cleaner alternative to lambdas |
Optional | Avoiding nulls in a functional way |
Characteristics of Functional Code
- No side effects.
- Pure functions.
- Immutability.
- Higher-order functions (accept or return other functions).
Tip
Be ready to:
- explain what FP is and how Java supports it since Java 8,
- show for-loop β stream().filter() conversion,
- mention lambda expressions and Streams API as key tools.
Parent: _Multithreading