Java 8 Streams API: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(6 intermediate revisions by the same user not shown)
Line 6: Line 6:


* [[Java#Java_8|Java]]
* [[Java#Java_8|Java]]
=TODO=
<font color=red>
* Process https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
</font>


=Overview=
=Overview=
Line 48: Line 56:


[[#Stream_Operations|Stream operations]] are composed into a ''stream pipeline'' to perform a computation.
[[#Stream_Operations|Stream operations]] are composed into a ''stream pipeline'' to perform a computation.
=<span id='Empty_Stream'></span><span id='From_Collections'></span><span id='Numeric_Ranges'></span><span id='From_Values'></span><span id='From_Arrays'></span><span id='From_Nullable'></span><span id='From_Files'></span><span id='From_Functions'></span>Stream Creation=
{{Internal|Java 8 Streams API Stream Creation|Stream Creation}}


=Stream Operation=
=Stream Operation=
Line 94: Line 106:
A ''reduction operation'' is an operation through which a stream is reduced to a value
A ''reduction operation'' is an operation through which a stream is reduced to a value


{{Internal|Java 8 Streams API - Reduction#Overview|Reduction}}
{{Internal|Java 8 Streams API - Reduction#Overview|Stream Reduction}}


===Stream-Level Predicates===
===Stream-Level Predicates===
Line 115: Line 127:


==Stateful Operations==
==Stateful Operations==
=Stream Creation=
==Empty Stream==
<syntaxhighlight lang='java'>
Stream<String> empty = Stream.empty();
</syntaxhighlight>
==From Collections==
Java 8 Collections expose a new <tt>stream()</tt> method that returns a stream.
==Numeric Ranges==
Specialized primitive interfaces IntStream and LongStream expose static <tt>[https://docs.oracle.com/javase/10/docs/api/java/util/stream/IntStream.html#range(int,int) range()]</tt> and <tt>[https://docs.oracle.com/javase/10/docs/api/java/util/stream/IntStream.html#rangeClosed(int,int) rangeClosed()]</tt> that return a sequential ordered specialized stream with values in the specified range.
<syntaxhighlight lang='java'>
IntStream is = IntStream.range(1, 11);
</syntaxhighlight>
==From Values==
The Stream class exposes static <tt>of()</tt> that builds a Stream instance from one argument or a variable list of arguments:
<syntaxhighlight lang='java'>
static <T> Stream<T> of​(T t);
static <T> Stream<T> of​(T... values);
</syntaxhighlight>
In the first case we'll get one element stream, and in the second, the stream will has as many elements as arguments. Of course, this API is practical to use for a small number of elements. To create a stream for a larger array, or when we need a stream of primitive types, see [[#From_Arrays|From Arrays]] below.
==From Arrays==
Arrays.stream() creates a stream from an array.
{{External|https://docs.oracle.com/javase/10/docs/api/java/util/Arrays.html#stream(T%5B%5D)}}
<syntaxhighlight lang='java'>
public static <T> Stream<T> stream​(T[] array);
</syntaxhighlight>
<tt>Arrays.stream()</tt> is better than <tt>[[#From_Values|Stream.of()]]</tt> when we need a primitive stream, because it will produce a specialized stream instead of autoboxing primitives into Java objects.
==From Nullable==
Since Java 9.
<syntaxhighlight lang='java'>
static <T> Stream<T> ofNullable​(T t);
</syntaxhighlight>
<syntaxhighlight lang='java'>
Stream<String> values = Stream.of("config", "home", "user").flatMap(key -> Stream.ofNullable(System.getProperty(key)));
</syntaxhighlight>
==From Files==
Many static methods  in <tt>java.nio.file.Files</tt> return a stream.
Note that the associated I/O resources must be closed to avoid leaks, and the streams are [[Java_7_try-with-resources#AutoCloseable|auto-closable]], so the following [[Java 7 try-with-resources#Overview|pattern]] may be used:
<syntaxhighlight lang='java'>
try(Stream<String> stream = Files.lines(new File(...).toPath())) {
    stream.forEach(...);
}
catch(IOException e) {
    // ...
}
</syntaxhighlight>
==From Functions==
Streams can be created from functions, which may result in ''infinite streams''.
===Stream.iterate()===
<tt>Stream.iterate()</tt> should be used when you need to produce a sequence of successive values, where a value depends on the previous one:
static <T> Stream<T> [https://docs.oracle.com/javase/10/docs/api/java/util/stream/Stream.html#iterate(T,java.util.function.UnaryOperator) iterate​](T seed, [[Java.util.function.UnaryOperator#Overview|UnaryOperator]]<T> f);
static <T> Stream<T> [https://docs.oracle.com/javase/10/docs/api/java/util/stream/Stream.html#iterate(T,java.util.function.Predicate,java.util.function.UnaryOperator) iterate​](T seed, Predicate<? super T> hasNext, [[Java.util.function.UnaryOperator#Overview|UnaryOperator]]<T> next)
===Stream.generate()===
static <T> Stream<T> [https://docs.oracle.com/javase/10/docs/api/java/util/stream/Stream.html#generate(java.util.function.Supplier) generate​]([[Java_8_Lambda_Expressions#Supplier|Supplier<? extends T>]] s)


=Short-Circuiting=
=Short-Circuiting=
Line 233: Line 157:
=Parallel Streams=
=Parallel Streams=


A ''parallel stream'' is a stream that splits its elements into multiple chunks, processing each chunk with a different thread. This way the workload is automatically partitioned on all cores of a multicore processor.
{{Internal|Java 8 Streams API - Parallel Streams|Parallel Streams}}
 
 
A parallel stream is produced by a collection by invoking its <tt>parallelStream()</tt> method. An existing stream can be parallelized by invoking <tt>parallel()</tt> on the stream itself. Calling <tt>parallel()</tt> on a sequential stream does not imply any concrete transformation on the stream itself. Internally, a boolean flag is set to signal that the subsequent operations should be run in parallel. A parallel stream can be turned into a sequential stream by invoking <tt>sequential()</tt>. The last call on <tt>parallel()</tt> or <tt>sequential()</tt> affects the pipeline globally, it is not possible to have parts of the pipeline executed in parallel and parts of it executed sequentially.
 
Parallel stream implementations uses [[Java_7_Fork/Join_Framework#Overview|fork/join framework]] introduced in Java 7, specifically the ForkJoinPool.
 
=TODO=
 
<font color=red>
 
* Process https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
* Explain how data is partitioned.
 
</font>

Latest revision as of 19:45, 6 April 2018

External

Internal

TODO

Overview

The Streams API provides a method to represent and process sequenced data in parallel, transparently taking advantage of multi-core architectures. The Streams API offers a higher level of abstraction than Java Collections, based on the concept of transforming a stream of objects into a stream of different objects, while delegating parallel processing concerns to the runtime. The Streams API obviates the need for explicitly programming Threads and the synchronized mechanism. This represents a shift to focusing on partitioning the data rather than coordinating access to it.

Stream

https://docs.oracle.com/javase/10/docs/api/java/util/stream/Stream.html

A stream is a sequence of data items of a specific type, which are conceptually produced one at a time by a source, and that supports sequential and parallel aggregate operations. There may be several intermediate operations, connected into a stream pipeline, and one terminal operation that executes the stream pipeline and produces a non-stream result.

All streams implement java.util.stream.Stream<T>.

Unlike collections, which are data structures for storing and accessing elements with specific time/space complexities, streams are about expressing computations on data. Unlike collections, which store all the values of the data structure in memory, and every element of the collection has to be computed before it can be added to collection, streams' elements are computed on demand. The Streams API uses behavior parameterization and expects code that parameterizes the behavior of its operations to be passed to the API. Collections are mostly about storing and accessing data, whereas the Streams API is mostly about describing computations on data.

A stream can be traversed only once. After it was traversed, a stream is said to be consumed. An attempt to consume it again throws:

java.lang.IllegalStateException: stream has already been operated upon or closed

The Streams API is meant as an alternative way of processing collections, in a declarative manner, by using internal iterations. Unlike in a collection's external iteration case, the loop over elements is managed internally inside the library. The API users provides a function specifying the computation that needs to be done.

Encounter Order

The encounter order specifies the order in which items logically appear in the stream. For example, a stream generated from a List will expose elements in the same order in which they appear in the List.

The fact that a stream has an encounter order or not depends on the source (List) and on the intermediate operations (sorted()). Some operations may render an ordered stream unordered (BaseStream.unordered()). unordered() makes sense because in cases where the stream has an encounter order, but the user does not care about it, explicit de-ordering may improve parallel performance for some stateful or terminal operations. For sequential streams, the presence or absence of an encounter order does not affect performance, only determinism. If a stream is ordered, repeated execution of identical stream pipelines on an identical source will produce an identical result; if it is not ordered, repeated execution might produce different results. For parallel streams, relaxing the ordering constraint can sometimes enable more efficient execution. Operations that are intrinsically tied to encounter order, such as limit(), may require buffering to ensure proper ordering, undermining the benefit of parallelism. Most stream pipelines, however, still parallelize efficiently even under ordering constraints.

Ordered Stream

An ordered stream is a stream that has a defined encounter order.

If a stream is ordered, most operations are constrained to operate on the elements in their encounter order.

Source

Stream data sources are collections, arrays and I/O resources. For more details on how streams are created see Stream Creation.

Data elements generated by an ordered collection will have the same order in the stream.

Stream Pipeline

Stream operations are composed into a stream pipeline to perform a computation.

Stream Creation

Stream Creation

Stream Operation

Stream operations have two important characteristics:

  1. Pipelining. Most stream operations return a stream, allowing operations to be chained and form a larger pipeline and enabling certain operations such as laziness and short-circuiting and
  2. Internal Iteration.

The stream operations that return a stream, and thus can be connected, are called intermediate operations. The stream operations that close the stream and return a non-stream result are called terminal operations.

Ideally, stream operations must be based on functions that don't interact - the encapsulated functionality must not access shared state. For more details see Functional Programming.

Intermediate Operations

A stream operation that returns another stream, and thus can be connected to other stream operations to form a pipeline, is called intermediate operation. Intermediate operations do not consume from streams, their purpose is to serve as a processing element in a pipeline. Intermediate operations do not perform any processing until a terminal operation is invoked on the stream pipeline. It is said that the intermediate operations are lazy.

The idea behind a stream pipeline is similar to the builder pattern.

Filtering Data

Filtering in this context means dropping certain elements based on a criterion.

Filtering Data with Java 8 Streams API

Transforming Data

Transforming Data with Java 8 Streams API

Sorting Data

Stream<T> sorted();

This form applies to streams whose elements have a natural order (they implement Comparable). If the elements of this stream are not Comparable, a java.lang.ClassCastException may be thrown when the terminal operation is executed.

Stream<T> sorted(Comparator<? super T> comparator);

Sorting operations are stateful unbouned.

Terminal Operations

A stream operation that consumes and closes the stream and returns a non-stream result is called terminal operation.

Reduction

A reduction operation is an operation through which a stream is reduced to a value

Stream Reduction

Stream-Level Predicates

These "match" operations apply a predicate to all elements of the stream, subject to short-circuiting, and return a boolean result.

Stream-Level Predicates

Find Methods

Find Methods

Other Terminal Operations

forEach() consumes each element from a stream and applies a lambda to each of them. The method returns void.

void forEach(Consumer<? super T> action);

Stateful Operations

Short-Circuiting

Some operations do not need to process the whole stream to produce a result. For example, the evaluation of anyMatch may stop at the first element that matches.

Autoboxing and Specialized Interfaces

The Streams API supplies primitive stream specializations that support specialized method to work with primitive types. These interfaces eliminate the need for autoboxing.

These interfaces bring new methods to perform common numeric reductions such as sum() and max(). In addition they have methods to convert back to a stream of objects when necessary. Example:

IntStream intStream = ...;
Stream<Integer> s = intStream.boxed();

Examples of specialized API: mapping and flat-mapping.

Specialized Interface Numeric Ranges

IntStream and LongStream expose static methods that generate numeric ranges: range() and rangeClosed().

Also see Numeric Ranges above.

Parallel Streams

Parallel Streams