Java 8 Stream and operation on arrays
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Java 8 brought significant enhancements to the Java programming language, one of the most important being the introduction of the Stream API. This API provides a powerful mechanism for processing sequences of elements and offers an abstraction level to enable functional-style operations on arrays and collections. The adoption of Streams has resulted in cleaner, more readable, and more concise code.
Introduction to Java 8 Streams
Streams are not a data structure but a sequence of data elements supporting sequential and parallel operations. These operations enable the processing of data declaratively and can effortlessly handle complex data transformations.
Key Characteristics of Streams
- No Storage: Streams hold no data; they are a view or pipeline over the data.
- Functional in Nature: Operations on streams produce results without modifying the underlying data source.
- Lazy Execution: Intermediate operations are lazy and only executed when a terminal operation is invoked.
- Possibility of Parallelism: Streams can be easily parallelized by calling the
.parallel()method.
Stream Operations
Streams support two types of operations:
- Intermediate Operations: These return a stream and support the creation of a pipeline. They're lazy and only executed when a terminal operation exists. Examples include
map(),filter(), anddistinct(). - Terminal Operations: These produce a result or a side-effect, such as collecting the elements to a list or finding an average. Examples include
collect(),forEach(), andreduce().
Streams and Arrays
While an array is not a Collection, you can construct a Stream from an array using Arrays.stream() or Stream.of().
Example: Working with Arrays and Streams
Consider the following examples that demonstrate how to transform, filter, and collect data:
This example demonstrates the following operations on an array:
- Filter: Excludes names with 3 or fewer characters.
- Sort: Arranges the remaining names in ascending order.
- Collect: Gathers the elements into a List.
Stream Pipeline
A stream pipeline consists of:
- Source: An array, collection, or I/O channel.
- Zero or more Intermediate Operations: Processes the elements into a new stream.
- Terminal Operation: Executes the pipeline and produces a result.
Advanced Examples and Use Cases
Summing Elements of an Integer Array
Using reduce(), you can sum the elements in a stream:
Finding Maximum Value
To find a maximum value in an array using streams:
Parallel Stream Example
Parallel streams can improve performance by dividing tasks into subtasks across multiple threads:
Summary Table
| Feature | Description |
| Source | Constructed from arrays, collections, or I/O channels. |
| Intermediate Ops | Includes operations like map, filter, and sorted. |
| Terminal Ops | Includes operations like collect, reduce, and forEach. |
| Lazy Execution | Intermediate operations are not executed until a terminal operation. |
| Stateless/Stateful | Intermediate operations may be stateless (e.g., map) or stateful. |
| Sequential/Parallel | Streams can be processed in parallel, improving performance. |
Conclusion
Java 8's Stream API is a powerful tool for processing sequences of data in a functional style. It inherently supports both sequential and parallel processing, making it an ideal choice for many applications. Its ease of use, coupled with enhanced performance when leveraged correctly, makes it an integral part of modern Java programming. As developers, understanding and employing streams effectively can lead to more efficient and expressive code.
Related reading
- Java 8 Streams - collect vs reduce
- Java 8 Streams multiple filters vs. complex condition
- Java HTTPS client certificate authentication
- Java List.contains(Object with field value equal to x)
- Java & RabbitMQ - Queueing & Multithreading - Or Couchbase as Job-Queue
- Java array reflection isArray vs. instanceof
- Java 8 Where is TriFunction and kin in java.util.function? Or what is the alternative?
- Java abstract interface

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.