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

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

Moderator: admin

Post Reply
webber
Posts: 18
Joined: Mon Apr 01, 2013 8:38 am
Contact:

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

Post by webber »

I don't no why this question is in the "parallel streams" section ?

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

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

Post by admin »

You are right. Fixed.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

nirwan84
Posts: 7
Joined: Thu Nov 19, 2020 3:43 pm
Contact:

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

Post 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] ?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

nirwan84
Posts: 7
Joined: Thu Nov 19, 2020 3:43 pm
Contact:

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

Post by nirwan84 »

Indeed, thanks!

61d14837
Posts: 13
Joined: Tue Jul 21, 2020 1:39 pm
Contact:

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

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

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

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

Post 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!
If you like our products and services, please help us by posting your review here.

edufin166@yahoo.com
Posts: 24
Joined: Wed Sep 28, 2022 9:41 am
Contact:

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

Post by edufin166@yahoo.com »

Why System.out.println(ls.stream().max((a, b)->a>b?a:b)); //4 is incorrect?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 40 guests