Java 8 Streams provide a powerful API for processing sequences of elements. Stream operations are divided into two categories: intermediate and terminal operations. Intermediate operations transform a stream and return another stream, allowing multiple operations to be chained together. Terminal operations produce a result or side-effect and complete the stream pipeline.
Intermediate Operations:
Intermediate operations return a new stream and are lazy, meaning they are not executed until a terminal operation is invoked.
| Operation | Syntax | Description | Example |
|---|---|---|---|
filter() |
stream.filter(predicate) |
Filters elements based on a condition | list.stream().filter(x -> x > 10) |
map() |
stream.map(function) |
Transforms each element into another form | list.stream().map(String::toUpperCase) |
flatMap() |
stream.flatMap(function) |
Flattens a stream of streams into a single stream | list.stream().flatMap(Collection::stream) |
distinct() |
stream.distinct() |
Removes duplicate elements from the stream | list.stream().distinct() |
sorted() |
stream.sorted() |
Sorts elements in natural order | list.stream().sorted() |
sorted() |
stream.sorted(comparator) |
Sorts elements using a custom comparator | list.stream().sorted((a, b) -> b - a) |
peek() |
stream.peek(consumer) |
Performs an action on each element (used for debugging) | stream.peek(System.out::println) |
limit() |
stream.limit(n) |
Truncates the stream to the first n elements | list.stream().limit(5) |
skip() |
stream.skip(n) |
Skips the first n elements | list.stream().skip(2) |
mapToInt() |
stream.mapToInt(ToIntFunction) |
Converts stream to IntStream |
list.stream().mapToInt(String::length) |
mapToDouble() |
stream.mapToDouble(ToDoubleFunction) |
Converts stream to DoubleStream |
list.stream().mapToDouble(Double::parseDouble) |
mapToLong() |
stream.mapToLong(ToLongFunction) |
Converts stream to LongStream |
list.stream().mapToLong(Long::parseLong) |
boxed() |
intStream.boxed() |
Converts primitive stream to Stream of wrapper objects | IntStream.range(1, 5).boxed() |
import java.util.*;
import java.util.stream.*;
public class StreamIntermediateExample {
public static void main(String[] args) {
List<String> names = Arrays.asList(
"Mahesh", "Paani", "Datta", "Mahesh", "Harsha", "Ganesh", "Ravi", "Maheshwar", "Ravi"
);
List<String> processedNames = names.stream()
.filter(name -> name.length() > 5) // Keep names longer than 5 characters
.distinct() // Remove duplicates
.sorted() // Sort alphabetically
.peek(name -> System.out.println("Filtered: " + name)) // Debugging
.map(String::toUpperCase) // Convert to uppercase
.limit(5) // Take first 5
.skip(1) // Skip the first of those
.collect(Collectors.toList()); // Collect result into a List
System.out.println("\nFinal Processed Names:");
processedNames.forEach(System.out::println);
}
}
/*
D:\java8streams>javac StreamIntermediateExample.java
D:\java8streams>java StreamIntermediateExample
Filtered: Ganesh
Filtered: Harsha
Filtered: Mahesh
Filtered: Maheshwar
Final Processed Names:
HARSHA
MAHESH
MAHESHWAR
*/
