1. Introduction to Java Streams
The Stream API, introduced in Java 8, is a powerful abstraction for processing collections of data in a functional-style. It enables operations on a sequence of elements (like filtering, mapping, and reducing) and supports parallel execution. Streams make it easier to write concise and efficient code, especially when working with large datasets or complex data processing pipelines.
2. Core Concepts of Java Streams
Java Streams are a series of elements that support aggregate operations. They:
Are not data structures, but rather views of data.
Do not modify the original data.
Use lazy evaluation to execute operations only when necessary.
Support pipelining of operations for cleaner code.
3. Creating Streams
Streams can be created from various sources such as collections, arrays, files, or even manually defined data.
Examples
// From a List
Stream<String> stream = List.of("apple", "banana", "cherry").stream();
// From an Array
Stream<Integer> arrayStream = Arrays.stream(new Integer[]{1, 2, 3, 4});
// From a File
Stream<String> fileStream = Files.lines(Paths.get("file.txt"));
// Using Stream.of
Stream<String> streamOfValues = Stream.of("one", "two", "three");
4. Finite Streams
A finite stream has a limited number of elements. Most collections (like lists and arrays) provide finite streams.
Stream<String> finiteStream = List.of("one", "two", "three").stream();
5. Infinite Streams
Infinite streams are unbounded, meaning they can generate an infinite sequence of elements, typically created with methods like Stream.iterate()
and Stream.generate()
.
Stream<Integer> infiniteStream = Stream.iterate(0, n -> n + 1);
Stream<Double> randomNumbers = Stream.generate(Math::random);
Infinite streams should generally be limited to avoid resource exhaustion.
6. Parallel Streams
Parallel streams allow for concurrent processing, improving performance for large datasets. Use .parallelStream()
on collections or .parallel()
on any stream to create a parallel stream.
List<String> data = Arrays.asList("apple", "banana", "cherry");
data.parallelStream().forEach(System.out::println);
7. Intermediate Operations with Streams
Intermediate operations transform a stream, returning another stream, and can be chained together. They are lazily evaluated, meaning they only execute when a terminal operation is applied.
filter(Predicate<T>)
Filters elements based on a predicate.
Stream<Integer> filtered = Stream.of(1, 2, 3, 4, 5).filter(n -> n % 2 == 0);
map(Function<T, R>)
Applies a function to each element, transforming it to a new type.
Stream<String> uppercased = Stream.of("apple", "banana").map(String::toUpperCase);
flatMap(Function<T, Stream<R>>)
Transforms each element into a stream and flattens the results.
Stream<String> flattened = Stream.of("apple banana", "cherry").flatMap(s -> Stream.of(s.split(" ")));
sorted(Comparator<T>)
Sorts elements based on a comparator.
Stream<Integer> sorted = Stream.of(3, 1, 2).sorted();
distinct()
Removes duplicate elements from the stream.
Stream<Integer> distinctValues = Stream.of(1, 2, 2, 3).distinct();
skip(long n)
Skips the first n
elements.
Stream<Integer> skipped = Stream.of(1, 2, 3, 4, 5).skip(2);
limit(long n)
Limits the stream to the first n
elements.
Stream<Integer> limited = Stream.of(1, 2, 3, 4, 5).limit(3);
Custom Intermediate Operations with Stream Gatherers
Stream gatherers are not predefined, but you can create custom logic with existing operations. For example, you could combine filter
, map
, and other operations to create a customized transformation pipeline.
8. Terminal Operations with Streams
Terminal operations execute the stream pipeline and produce a result.
collect()
Collects elements into a collection or other data structure.
List<Integer> collectedList = Stream.of(1, 2, 3).collect(Collectors.toList());
forEach(Consumer<T>)
Performs an action on each element.
Stream.of(1, 2, 3).forEach(System.out::println);
reduce(BinaryOperator<T>)
Reduces the elements to a single result.
int sum = Stream.of(1, 2, 3).reduce(0, Integer::sum);
count()
, average()
, min()
, max()
Aggregation functions.
long count = Stream.of(1, 2, 3).count();
int max = Stream.of(1, 2, 3).max(Integer::compare).orElse(0);
findFirst()
, findAny()
Finds the first or any element.
Optional<Integer> first = Stream.of(1, 2, 3).findFirst();
allMatch(Predicate)
, anyMatch(Predicate)
, noneMatch(Predicate)
Checks if all, any, or none of the elements match a condition.
boolean allEven = Stream.of(2, 4, 6).allMatch(n -> n % 2 == 0);
9. Dealing with null
in Streams
Streams donβt handle null
values gracefully. Avoid null
elements by removing or transforming them. For instance, you can filter null
elements with filter(Objects::nonNull)
.
Stream.of(1, null, 3)
.filter(Objects::nonNull)
.forEach(System.out::println);
10. Conclusion
The Java Stream API is a powerful toolkit for handling data in a functional style, making your code more readable and concise. This guide covered the core aspects of Java streams, from creation and transformations to aggregation and filtering. By understanding and applying these concepts, youβll be well-equipped to process complex data more effectively. Use streams responsibly and remember that parallel streams are not always faster; test thoroughly to ensure efficiency.
More such articles: