Java Reference
In-Depth Information
The current solution still doesn't work! This is because you now end up with a list of streams
(more precisely, Stream<Stream<String>>)! Indeed, you first convert each word into an array
of its individual letters and then make each array into a separate stream.
Using flatMap
You can fix this problem by using flatMap as follows:
Using the flatMap method has the effect of mapping each array not with a stream but
with the
contents of that stream
. All the separate streams that were generated when using
map(Arrays::stream) get amalgamated—flattened into a single stream.
Figure 5.6
illustrates the
effect of using the flatMap method. Compare it with what map does in
figure 5.5
.
Search WWH ::
Custom Search