Java 8 Features: Lambdas, Streams, and Functional Interfaces
Java 8 introduced major enhancements that brought functional programming concepts into the language. The three most important additions are:
Lambda Expressions
Streams API
Functional Interfaces
Together, these features allow Java developers to write cleaner, more concise, and more expressive code.
1. Lambda Expressions
What is a Lambda?
A lambda expression is a short block of code that:
Can be passed around as a method argument
Has no name
Implements a functional interface method
Basic Syntax
(parameters) -> expression
or
(parameters) -> { statements; }
Example: Before Java 8
Using an anonymous class:
Runnable r = new Runnable() {
public void run() {
System.out.println("Hello from Runnable!");
}
};
Example: With Lambda
Runnable r = () -> System.out.println("Hello from Runnable!");
Common Lambda Examples
With multiple parameters
(int a, int b) -> a + b
With a block
(name) -> {
System.out.println("Hello " + name);
}
2. Functional Interfaces
What is a Functional Interface?
A functional interface has exactly one abstract method.
It can have default and static methods as well.
Examples in Java 8:
Runnable — void run()
Callable<T> — T call()
Comparator<T> — int compare(T a, T b)
Predicate<T> — returns true/false
Function<T, R> — transforms a value
Consumer<T> — takes input, returns nothing
Supplier<T> — returns a value
Annotated Functional Interface
@FunctionalInterface
interface Greeting {
void sayHello(String name);
}
Using with Lambda
Greeting g = (name) -> System.out.println("Hello " + name);
g.sayHello("Alice");
3. Streams API
The Streams API is one of the most powerful features of Java 8. It allows you to process collections of data in a functional and declarative way.
Key Concepts
Stream is not a data structure
It doesn’t store data; it processes data from collections, arrays, or I/O channels.
Operations
There are two kinds:
(1) Intermediate operations (return a stream):
map()
filter()
sorted()
distinct()
limit()
(2) Terminal operations (produce a result):
collect()
forEach()
reduce()
count()
findFirst(), findAny()
Stream Processing Pipeline
A stream pipeline typically has three parts:
Source (e.g., list, array)
Intermediate operations (transform)
Terminal operation (produce result)
Example: Without Streams
Filter even numbers and print them:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
for (Integer n : numbers) {
if (n % 2 == 0) {
System.out.println(n);
}
}
With Streams
numbers.stream()
.filter(n -> n % 2 == 0)
.forEach(System.out::println);
More Stream Examples
Mapping
List<String> upper = list.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
Sorting
list.stream()
.sorted()
.forEach(System.out::println);
Reducing
int sum = numbers.stream()
.reduce(0, (a, b) -> a + b);
Collecting
List<String> result = strings.stream()
.filter(s -> s.startsWith("A"))
.collect(Collectors.toList());
Benefits of Java 8 Functional Features
Cleaner and more readable code
Less boilerplate (especially with lambdas)
Easier to parallelize work using parallelStream()
More declarative style
Better use of modern multicore processors
Summary
Feature Purpose Example
Lambdas Concise functions as values x -> x * 2
Functional Interfaces Enable lambdas Predicate<T>
Streams API Functional data processing list.stream().filter(...).collect(...)
Java 8’s functional programming features significantly modernized the language and made it more expressive and powerful.
Learn Full Stack JAVA Course in Hyderabad
Read More
Java Collections Framework: List, Set, Map
Java Data Types and Variables Explained
Object-Oriented Programming in Java
Visit Our Quality Thought Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments