2nd Option Explanation 5th point - needs correction.:
"5. The value is actually an object of type List<String>, which is printed in the output. Since there are two groups, two lists are printed. First list has only one Course element and the second list has three."
Correction:
collect method returns Map<String, List<Course>>, so, value is List<Course>.
About Question enthuware.ocpjp.v8.2.1836 :
Moderators: Site Manager, fjwalraven
-
- Posts: 6
- Joined: Tue Oct 20, 2015 9:47 am
- Contact:
-
- Site Admin
- Posts: 10389
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1836 :
Actually, 5 is in relation to 4.
The fact that collect returns a Map is already described in point 2:4. The given code provides a trivial lambda expression for BiConsumer that just prints the second parameter, which happens to be the value part of of the key-value pair of the Map.
5. The value is actually an object of type List<String>, which is printed in the output. Since there are two groups, two lists are printed. First list has only one Course element and the second list has three.
2. It is important to understand that the return type of the collect method depends on the Collector that is passed as an argument. In this case, the return type would be Map<K, List<T> because that is the type specified in the Collector returned by the groupingBy method.
-
- Posts: 27
- Joined: Tue Sep 22, 2015 1:01 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1836 :
I think that in2xes is correct. The value of the map is List<Course>, not List<String>.
I have changed toString method in Course class to:
After it, the result is:
I have changed toString method in Course class to:
Code: Select all
public String toString(){
return "Course "+ id+" "+category;
}
So, forEach prints courses data via toString, not simple List<String>.[Course C# C#]
[Course OCAJP Java, Course OCPJP Java, Course OCEJPA Java]
-
- Site Admin
- Posts: 10389
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1836 :
Oh yes! Not sure why I didn't see that 
Fixed.
thank you for your feedback!

Fixed.
thank you for your feedback!
Who is online
Users browsing this forum: No registered users and 2 guests