About Question enthuware.ocpjp.v8.2.1836 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
in2xes
Posts: 6
Joined: Tue Oct 20, 2015 9:47 am
Contact:

About Question enthuware.ocpjp.v8.2.1836 :

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

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

Re: About Question enthuware.ocpjp.v8.2.1836 :

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

lveto15
Posts: 27
Joined: Tue Sep 22, 2015 1:01 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1836 :

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

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

Re: About Question enthuware.ocpjp.v8.2.1836 :

Post by admin »

Oh yes! Not sure why I didn't see that :(
Fixed.
thank you for your feedback!

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests