Java 8 Features
In this lesson: Top Java 8 features — lambda expressions, Stream API, functional interfaces, Optional, default methods — examples.
What Java 8 Added
The five features you must know are: lambda expressions, functional interfaces, the Stream API, default methods, and Optional. Let's see each with a working example.
1. Lambda Expressions
A lambda expression is a short, anonymous function — a block of code you can pass around like a value. It removes the need for bulky anonymous classes. The syntax is (parameters) -> { body }.
// Old way (before Java 8) Runnable r1 = new Runnable() { public void run() { System.out.println("Hello"); } }; // Java 8 lambda — one line Runnable r2 = () -> System.out.println("Hello"); r2.run();
Hello
2. Functional Interfaces
A functional interface is an interface with exactly one abstract method. Lambdas can only be used with functional interfaces. Java marks them with the @FunctionalInterface annotation. Common built-in ones are Runnable, Comparator, Predicate and Function.
// Predicate takes a value and returns true/false import java.util.function.Predicate; Predicate<Integer> isEven = n -> n % 2 == 0; System.out.println(isEven.test(10)); // true System.out.println(isEven.test(7)); // false
true false
3. Stream API
The Stream API lets you process a collection (like a list) as a pipeline of operations — filter, map, sort, collect — without writing loops. Streams are lazy and readable.
import java.util.*; import java.util.stream.*; List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5, 6); // keep even numbers, double them, collect to a list List<Integer> result = nums.stream() .filter(n -> n % 2 == 0) .map(n -> n * 2) .collect(Collectors.toList()); System.out.println(result);
[4, 8, 12]
4. Default Methods
Before Java 8, interfaces could not have method bodies. Default methods allow an interface to provide a ready-made implementation using the default keyword. This let Java add new methods to old interfaces without breaking existing code.
interface Greet { default void hello() { System.out.println("Hello from default method"); } } class Demo implements Greet {} // Demo gets hello() for free new Demo().hello();
Hello from default method
5. Optional
Optional is a container that may or may not hold a value. It helps you avoid the dreaded NullPointerException by forcing you to handle the "no value" case explicitly.
import java.util.Optional; Optional<String> name = Optional.ofNullable(null); System.out.println(name.orElse("Guest")); // safe default
Guest
Quick Summary Table
| Feature | Purpose | Keyword / Class |
|---|---|---|
| Lambda | Short anonymous function | -> |
| Functional Interface | Interface with one abstract method | @FunctionalInterface |
| Stream API | Process collections in a pipeline | .stream() |
| Default Method | Method body inside an interface | default |
| Optional | Avoid null pointer errors | Optional |
Lambdas + streams together let you replace long for loops with one readable line. This is now the standard style in modern Java and is a very common interview topic.
A stream can be used only once. After a terminal operation like collect(), you cannot reuse the same stream — create a new one from the source.
Summary
- Java 8 (2014) added functional programming to Java.
- Lambda = short anonymous function using
->. - Functional interface = one abstract method; enables lambdas.
- Stream API processes collections as a pipeline (filter, map, collect).
- Default methods add bodies to interfaces; Optional avoids null errors.
