Java Series #7: Functional Programming & Java 8 - The Big Shift
Mastering Java 8 Functional Programming — Lambdas, Streams, and Modern Java Features

This is perfect for a full blog post covering functional programming, lambdas, functional interfaces, and the Java-8 ecosystem.
1. Why Java 8 Was a Big Deal
Java 8 (March 2014) marked the biggest transformation in Java since its creation.
It pushed Java from object-oriented style to a hybrid functional programming model.
Key goals:
Reduce boilerplate
Enable clean, expressive code
Improve performance using parallelism
Add modern APIs (Streams, Optional, Date/Time)
2. Lambda Expressions
What are Lambdas?
Anonymous functions
No class needed
Implement functional interfaces directly
Syntax:
parameters -> expression parameters -> { multiple statements }
Why Lambdas? Before vs After
Before Java 8 (Anonymous class)
Runnable r = new Runnable() {
public void run() {
System.out.println("Task running");
}
};
With Lambda (Java 8)
Runnable r = () -> System.out.println("Task running");
Clean. Short. Functional.
Types of Lambda Forms
() -> System.out.println("No parameters")
(x) -> x * x
(x, y) -> x + y
() -> { System.out.println("Multiple lines"); return 10; }
Where Are Lambdas Used?
Streams (filter, map, reduce)
Collections (sort, removeIf, forEach)
Threads (Runnable)
Event-driven programming
3. Functional Interfaces
A functional interface contains exactly one abstract method.
They may have default and static methods.
Marked with:
@FunctionalInterface
Why Functional Interfaces?
Because lambdas need a target type to attach to.
3.1 Java’s Core Functional Interfaces (java.util.function)
Predicate
- One argument → returns boolean
Predicate<Integer> isEven = n -> n % 2 == 0;
Consumer
- Takes input → returns nothing
Consumer<String> print = s -> System.out.println(s);
Supplier
- No input → returns a value
Supplier<Double> random = () -> Math.random();
Function<T, R>
- Takes input → returns output
Function<String, Integer> length = s -> s.length();
BiFunction<T, U, R>
BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
UnaryOperator
- Input and output same type
UnaryOperator<Integer> sq = n -> n*n;
BinaryOperator
- Two args, same type
BinaryOperator<Integer> max = (a, b) -> a > b ? a : b;
Functional Interfaces Table (With Meaning + Examples)
| Interface | Purpose | Example |
| Runnable | Task for thread execution | () -> System.out.println("run") |
| Comparable | Natural sorting | o1.compareTo(o2) |
| ActionListener | Event handling | e -> handle(e) |
| Callable | Thread task returning a value | () -> 42 |
| Predicate | Boolean test | x -> x > 10 |
| Consumer | Takes input, no return | x -> System.out.println(x) |
| Supplier | Provides value | () -> new Date() |
| Function | Input → Output | x -> x.length() |
| BiFunction | Two inputs → Output | (x,y) -> x+y |
| UnaryOperator | Same type unary | x -> x*x |
| BinaryOperator | Same type binary | (x,y)->x*y |
4. Default and Static Methods in Interfaces
Java 8 allowed:
default methods (with body)
static methods (with body)
Why?
To evolve interfaces without breaking existing code.
Example
interface Vehicle {
default void start() { System.out.println("Default start"); }
static void clean() { System.out.println("Cleaning..."); }
}
5. Streams API
Streams allow functional operations over collections.
Core Operations
filter()
map()
reduce()
sorted()
forEach()
collect()
Example: Filter + Map + Collect
List<Integer> nums = Arrays.asList(1,2,3,4,5);
List<Integer> sqEvens =
nums.stream()
.filter(n -> n % 2 == 0)
.map(n -> n * n)
.collect(Collectors.toList());
Parallel Streams
nums.parallelStream().forEach(System.out::println);
6. Optional Class
Solves NullPointerException problems.
Example
Optional<String> name = Optional.ofNullable(getName());
name.ifPresent(System.out::println);
Why Optional?
Represents missing values safely
Encourages clean, null-safe code
Avoids nested null checks
7. Enhanced Collection APIs
New methods in Java 8:
| Method | Meaning |
forEach() | Iterate easily |
removeIf() | Remove based on condition |
replaceAll() | Modify all elements |
sort() | New comparator using lambdas |
Example
list.removeIf(x -> x < 0);
8. Method Reference (::)
Short-hand for lambdas.
Example
list.forEach(System.out::println);
Types:
Static method reference
Instance method reference
Constructor reference
9. New Date and Time API (java.time)
Replaces old Date/Calendar.
Examples
LocalDate.now();
LocalDate.of(2024, 5, 20);
LocalDateTime.now();
Duration.between(t1, t2);
Modern, immutable, thread-safe.
10. Concurrency Enhancements
CompletableFuture
Asynchronous programming made easy.
CompletableFuture.supplyAsync(() -> "Hello")
.thenAccept(System.out::println);
Putting It All Together — Mini Example
Task: Get names starting with "A", uppercase them, sort, print.
List<String> names = Arrays.asList("Aadi", "Sam", "Arun", "Bob");
names.stream()
.filter(n -> n.startsWith("A"))
.map(String::toUpperCase)
.sorted()
.forEach(System.out::println);
Interview Questions (Java 8 – Functional Programming)
1. What is a functional interface?
An interface with exactly one abstract method. Used for lambda expressions.
2. Difference between Anonymous Class and Lambda?
Lambda has no identity/class
Lambdas are functional and concise
Anonymous classes allow multiple methods, lambdas do not
3. Why Optional?
To avoid null pointer exceptions and represent absence of values in a safe way.
4. Difference between Predicate and Function?
Predicate → boolean result
Function → returns any value type
5. What is lazy evaluation in Streams?
Stream intermediate operations run only when a terminal operation is invoked.
6. Why default methods in interfaces?
To add new behaviors without breaking old implementation classes.
About the author
- I’m a Full-stack Developer who enjoys turning ideas into simple and useful experiences. I like building clean user interfaces, exploring AI, and understanding how things work behind the scenes. From creating websites to trying out new technologies, I’m always curious and learning something new. Next.js, Node.js, MongoDB, and Java Spring Boot are the tools I use regularly. I enjoy experimenting with projects, sharing what I learn, and improving with every build. Vibing + Thinking.
Comments
Join the discussionNo comments yet. Be the first to comment.




