Page 1 of 1

1Z0-809 : About enthuware.ocpjp.v8.2.1773

Posted: Sun Mar 04, 2018 1:17 am
by stefanbanu
hi,

I got a question:

Given:
List<String> l1 = Arrays.asList("a", "b");
List<String> l2 = Arrays.asList("1", "2");

Which of the following lines of code will print the following values?
a
b
1
2

The correct answer is this one: Stream.of(l1, l2).flatMap((x)->x.stream()).forEach((x)->System.out.println(x));

but when I tried on my ide I'm still getting this result:
[a, b]
[1, 2]

the result that is the same as this one: Stream.of(l1, l2).forEach((x)->System.out.println(x));
[a, b]
[1, 2]
which I choose in first place.

so, what is the correct answer in the end?

thank you,
Stefan

Re: 1Z0-809

Posted: Sun Mar 04, 2018 8:28 am
by admin
Well, as you have mentioned, the output of options 1 and 2 is not what the question is asking about. Option 3, which is set as the right option produces the required output -
a
b
1
2
This is also explained in the explanation.

HTH,
Paul.

Re: 1Z0-809 : About enthuware.ocpjp.v8.2.1773

Posted: Sun Mar 04, 2018 10:09 pm
by stefanbanu
thank you :)

Re: 1Z0-809 : About enthuware.ocpjp.v8.2.1773

Posted: Mon Aug 26, 2019 12:42 pm
by sir_Anduin@yahoo.de
in answer 3 (Stream.of(l1, l2).flatMap((x)->Stream.of(x)).forEach((x)->System.out.println(x));)
Stream.of(x) will create a stream of List<String>, but wenn need a stream of String