About Question enthuware.ocpjp.ii.v11.2.1792 :
Moderator: admin
-
- Posts: 18
- Joined: Mon Apr 01, 2013 8:38 am
- Contact:
About Question enthuware.ocpjp.ii.v11.2.1792 :
I don't no why this question is in the "parallel streams" section ?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.1792 :
You are right. Fixed.
thank you for your feedback!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Thu Nov 19, 2020 3:43 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.1792 :
"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] ?
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] ?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.1792 :
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.
-
- Posts: 7
- Joined: Thu Nov 19, 2020 3:43 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.1792 :
Indeed, thanks!
-
- Posts: 13
- Joined: Tue Jul 21, 2020 1:39 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.1792 :
Regarding the return value of the compare method:
Java API says
Java API says
but the answer explains it asthe value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y
which is correct?the compare method returns a difference between two numbers.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.1792 :
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!
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.
-
- Posts: 24
- Joined: Wed Sep 28, 2022 9:41 am
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.1792 :
Why System.out.println(ls.stream().max((a, b)->a>b?a:b)); //4 is incorrect?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.1792 :
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.
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.
Who is online
Users browsing this forum: Bing [Bot] and 112 guests