The map method is meant to replace one element of stream with another. While the flatMap method replaces one element of a stream with elements of a new stream generated using the original element. Therefore, map is not useful here.
The above quote is from the 2nd answer choice. What did you mean when you said "The map method is meant to replace one element of stream with another."?
This answer choice is wrong because the output of map will be a stream and each entry in the stream is in the form of String[], not individual strings.
Is that correct?
What we want out of a mapping in this question is individual strings leaving the mapping. We then want to apply some conditions to those strings leaving the mapping in order to do filtering prior to choosing only distinct items.
Is my understanding correct?
Thanks.
Schmichael
Last edited by admin on Wed Jun 08, 2016 2:12 am, edited 1 time in total.
Reason:Removed multiple question marks
schchen2000 wrote:
What did you mean when you said "The map method is meant to replace one element of stream with another."?
For example, If your original stream has elements 1, 2, 3. You can use the map method to replace the elements in that stream with 2, 4, 6. So each element is being replaced by another element (*2).
BTW, this is just FYI: when you write multiple question marks (????), it conveys that you feel that the statement is completely nonsensical and is considered a bit rude. I know you didn't intend it that way so I have edited it and kept only one question mark.
This answer choice is wrong because the output of map will be a stream and each entry in the stream is in the form of String[], not individual strings.
Is that correct?
Correct.
What we want out of a mapping in this question is individual strings leaving the mapping. We then want to apply some conditions to those strings leaving the mapping in order to do filtering prior to choosing only distinct items.
This answer choice is wrong because the output of map will be a stream and each entry in the stream is in the form of String[], not individual strings.
String sentence1 = "Carpe diem. Seize the day, boys. Make your lives extraordinary.";
String sentence2 = "Frankly, my dear, I don't give a damn!";
String sentence3 = "Do I look like I give a damn?";
List<String> sentences = Arrays.asList(sentence1, sentence2, sentence3);
sentences.stream()
.map(str->Stream.of(str.split("[ ,.!?\r\n]")))
.peek(x -> x.forEach(System.out::print))
.count();
Hi Paul,
If there is not Terminal operation in these streams, (flatMap, filter and distinct they are intermediate) that means the stream is never executed, so it is like "nothing is done".
But why is still the good answer the first one? (I had correct)
Thank you
"Nothing is done" in the sense that the elements of the original stream are not processed and the elements for the new stream are not generated yet. But the new stream (with distinct elements) is indeed created (its elements are not decided yet). If you apply a terminal operation on strm, you will get distinct elements as required by the problem statement.