@ Carey Brown
Here is a solution:
NOTES:
map: Transforms
Data to a
String (reason) [EDIT: returns a stream of
Data to stream of
String]
map: Transforms
String (reason) to arrays of
Strings with two elements each [EDIT: returns stream of two arrays]
flatMap: Transforms the elements of two streams (from two arrays to two streams) to a single stream with all the elements
filter: This one is easy.
collect: The resulting (filtered) reason string element is written to a
List collection of type
String.
map,
flatMap and
filter are intermediate stream operations. The
peek() method I had mentioned in the earlier message is also an intermediate operation.
collect is a terminal operation; this is a reduction operation The stream is reduced to a collection.
One can introduce
peek() method in-between the map/flatmap/filter methods in the above code and see the data transformation. You see the data gets transformed as it flows thru the stream This is one of the main features of the streams. Also note that fliter, map, and reduce (in this case collect) are the main and most commonly used functions of stream operations.
Note the code posted by the OP:
I was hoping OP would try little bit and post back some reply. The above code doesn't compile.
- The statement
sentence.contains(c.toString()) is not valid. See the NOTES above for details.
-
filter returns a
Stream. Note that the intermediate stream operations (in this example, map, flatMap, filter and peek) return a
Stream.
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer