Streams from Collections – Streams
Streams from Collections
The default methods stream() and parallelStream() of the Collection interface create streams with collections as the data source. Collections are the only data source that provide the parallelStream() method to create a parallel stream directly. Otherwise, the parallel() intermediate operation must be used in the stream pipeline.
The following default methods for building streams from collections are defined in the java.util.Collection interface:
default Stream<E> stream()
default Stream<E> parallelStream()
Return a finite sequential stream or a possibly parallel stream with this collection as its source, respectively. Whether it is ordered or not depends on the collection used as the data source.
We have already seen examples of creating streams from lists and sets, and several more examples can be found in the subsequent sections.
The code below illustrates two points about streams and their data sources. If the data source is modified before the terminal operation is initiated, the changes will be reflected in the stream. A stream is created at (2) with a list of CDs as the data source. Before a terminal operation is initiated on this stream at (4), an element is added to the underlying data source list at (3). Note that the list created at (1) is modifiable. The count() operation correctly reports the number of elements processed in the stream pipeline.
List<CD> listOfCDS = new ArrayList<>(List.of(CD.cd0, CD.cd1)); // (1)
Stream<CD> cdStream = listOfCDS.stream(); // (2)
listOfCDS.add(CD.cd2); // (3)
System.out.println(cdStream.count()); // (4) 3
// System.out.println(cdStream.count()); // (5) IllegalStateException
Trying to initiate an operation on a stream whose elements have already been consumed results in a java.lang.IllegalStateException. This case is illustrated at (5). The elements in the cdStream were consumed after the terminal operation at (4). A new stream must be created on the data source before any stream operations can be run.
To create a stream on the entries in a Map, a collection view can be used. In the code below, a Map is created at (1) and populated with some entries. An entry view on the map is obtained at (2) and used as a data source at (3) to create an unordered sequential stream. The terminal operation at (4) returns the number of entries in the map.
Map<Integer, String> dataMap = new HashMap<>(); // (1)
dataMap.put(1, “en”); dataMap.put(2, “to”);
dataMap.put(3, “tre”); dataMap.put(4, “fire”);
long numOfEntries = dataMap
.entrySet() // (2)
.stream() // (3)
.count(); // (4) 4
In the examples in this subsection, the call to the stream() method can be replaced by a call to the parallelStream() method. The stream will then execute in parallel, without the need for any additional synchronization code (p. 1009).
Leave a Reply