Page 1 of 1

About Question enthuware.ocpjp.ii.v11.2.1792 :

Posted: Thu Jun 25, 2020 10:12 am
by webber
I don't no why this question is in the "parallel streams" section ?

Re: About Question enthuware.ocpjp.ii.v11.2.1792 :

Posted: Thu Jun 25, 2020 11:47 am
by admin
You are right. Fixed.
thank you for your feedback!

Re: About Question enthuware.ocpjp.ii.v11.2.1792 :

Posted: Sat Nov 21, 2020 1:34 pm
by nirwan84
"The code will print: 9 3 9 Optional[3]"

Shouldn't it be Optional[9] for the line:
System.out.println(ls.stream().max((a, b)->a>b?a:b)); //4
and not Optional[3] ?

Re: About Question enthuware.ocpjp.ii.v11.2.1792 :

Posted: Sun Nov 22, 2020 11:31 am
by admin
No, this is explained in detail in point 2 of the explanation:
. The Stream.max method requires a Comparator. All you need to implement this interface using a lambda expression is a reference to any method that takes two arguments and returns an int. The name of the method doesn't matter. That is why it is possible to pass the reference of Integer's max method as an argument to Stream's max method. However, Integer.max works very differently from Integer.compare. The max method returns the maximum of two numbers while the compare method returns a difference between two numbers. Therefore, when you pass Integer::max to Stream's max, you will not get the correct maximum element from the stream. That is why //2 will compile but will not work correctly. //4 is basically same as //2. It will not work correctly for the same reason.

Re: About Question enthuware.ocpjp.ii.v11.2.1792 :

Posted: Sun Nov 22, 2020 3:08 pm
by nirwan84
Indeed, thanks!

Re: About Question enthuware.ocpjp.ii.v11.2.1792 :

Posted: Tue Apr 20, 2021 7:41 am
by 61d14837
Regarding the return value of the compare method:
Java API says
the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y
but the answer explains it as
the compare method returns a difference between two numbers.
which is correct?

Re: About Question enthuware.ocpjp.ii.v11.2.1792 :

Posted: Wed Apr 21, 2021 7:21 am
by admin
It is true that the JavaDoc API promises only a value that is less than 0, 0, or greater than zero. The explanation is just trying to explain the actual value that is returned by Integer's compare (as per code) to explain why the given code will not work.
The explanation has been updated to make it clear.

thank you for your feedback!

Re: About Question enthuware.ocpjp.ii.v11.2.1792 :

Posted: Sat Oct 22, 2022 3:49 pm
by edufin166@yahoo.com
Why System.out.println(ls.stream().max((a, b)->a>b?a:b)); //4 is incorrect?

Re: About Question enthuware.ocpjp.ii.v11.2.1792 :

Posted: Sat Oct 22, 2022 8:35 pm
by admin
Because of the exact same reason as option no. 2. Did you understand why no. 2 is incorrect?

The lambda in no. 4 returns the max of two numbers (same as no. 2.). But a function returning max of two numbers cannot help you find max in a list of numbers. For that, you need a function that "compares" two numbers.

Please go through the detailed explanation of option 2 that explains why max is not the same as conparison . The same applies to option 4.