Page 1 of 1

About Question enthuware.ocpjp.v8.2.1902 :

Posted: Wed Jun 08, 2016 1:09 am
by schchen2000
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."?

Code: Select all

Stream<String> strm = sentences.stream()
.map(str->Stream.of(str.split("[ ,.!?\r\n]")))
.filter(s->s.length()>0).distinct();
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

Re: About Question enthuware.ocpjp.v8.2.1902 :

Posted: Wed Jun 08, 2016 2:12 am
by admin
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.

Code: Select all

Stream<String> strm = sentences.stream()
.map(str->Stream.of(str.split("[ ,.!?\r\n]")))
.filter(s->s.length()>0).distinct();
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.

Is my understanding correct?

Thanks.

Schmichael
Correct.

Re: About Question enthuware.ocpjp.v8.2.1902 :

Posted: Wed Jun 08, 2016 10:57 pm
by schchen2000
Thanks a lot. Sorry about ????. I'll be more aware next time.

Schmichael

Re: About Question enthuware.ocpjp.v8.2.1902 :

Posted: Fri Feb 07, 2020 12:46 pm
by teodorj
admin wrote:
Wed Jun 08, 2016 2:12 am
schchen2000 wrote:

Code: Select all

Stream<String> strm = sentences.stream()
.map(str->Stream.of(str.split("[ ,.!?\r\n]")))
.filter(s->s.length()>0).distinct();
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.
Executing this code:

Code: Select all

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();	
Will print:

CarpediemSeizethedayboysMakeyourlivesextraordinaryFranklymydearIdon'tgiveadamnDoIlooklikeIgiveadamn.

It seems that each element in stream using map contains each words not String[] because it uses Stream.of(T...) (with varargs version).

Therefore, Option2 is wrong because Stream has no length() method and actually will not compile.

Re: About Question enthuware.ocpjp.v8.2.1902 :

Posted: Sat Feb 08, 2020 1:18 am
by admin
Hi Teodorj, You are right. I totally missed this point. Thanks for correcting!
Paul.

Re: About Question enthuware.ocpjp.v8.2.1902 :

Posted: Mon Aug 24, 2020 8:08 am
by Javier
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

Re: About Question enthuware.ocpjp.v8.2.1902 :

Posted: Mon Aug 24, 2020 8:52 am
by admin
"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.