Page 1 of 1
About Question enthuware.ocpjp.v8.2.1836 :
Posted: Sat Oct 24, 2015 3:12 pm
by in2xes
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>.
Re: About Question enthuware.ocpjp.v8.2.1836 :
Posted: Sat Oct 24, 2015 9:28 pm
by admin
Actually, 5 is in relation to 4.
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.
The fact that collect returns a Map is already described in point 2:
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.
Re: About Question enthuware.ocpjp.v8.2.1836 :
Posted: Tue Oct 27, 2015 7:55 am
by lveto15
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:
Code: Select all
public String toString(){
return "Course "+ id+" "+category;
}
After it, the result is:
[Course C# C#]
[Course OCAJP Java, Course OCPJP Java, Course OCEJPA Java]
So, forEach prints courses data via toString, not simple List<String>.
Re: About Question enthuware.ocpjp.v8.2.1836 :
Posted: Tue Oct 27, 2015 10:02 pm
by admin
Oh yes! Not sure why I didn't see that

Fixed.
thank you for your feedback!