About Question enthuware.ocpjp.v8.2.1902 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

About Question enthuware.ocpjp.v8.2.1902 :

Post 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
Last edited by admin on Wed Jun 08, 2016 2:12 am, edited 1 time in total.
Reason: Removed multiple question marks

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

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

Post by schchen2000 »

Thanks a lot. Sorry about ????. I'll be more aware next time.

Schmichael

teodorj
Posts: 28
Joined: Tue Jun 19, 2018 10:27 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Hi Teodorj, You are right. I totally missed this point. Thanks for correcting!
Paul.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests