About Question enthuware.ocpjp.v8.2.1802 :

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

Moderator: admin

Post Reply
lenalena
Posts: 56
Joined: Tue Feb 21, 2017 4:24 pm
Contact:

About Question enthuware.ocpjp.v8.2.1802 :

Post by lenalena »

Please, explain why option 1 is wrong. I can see that options 2 and 3 have wrong syntax, and option 4 is obviously correct, but what would be the "official" reason for option 1 being wrong?

When I tried to compile the code with option 1 I got
Student.java:26: error: incompatible types: inference variable T#1 has incompatible bounds
Map<Integer, List<Student>> grouping = ls.stream().collect( Collectors.groupingBy(Student::getMarks,Collectors.mapping(Student::getName, Collectors.toList())) );
^
equality constraints: Student
lower bounds: String,U
where T#1,U,T#2,A,R are type-variables:
T#1 extends Object declared in method <T#1>toList()
U extends Object declared in method <T#2,U,A,R>mapping(Function<? super T#2,? extends U>,Collector<? super U,A,R>)
T#2 extends Object declared in method <T#2,U,A,R>mapping(Function<? super T#2,? extends U>,Collector<? super U,A,R>)
A extends Object declared in method <T#2,U,A,R>mapping(Function<? super T#2,? extends U>,Collector<? super U,A,R>)
R extends Object declared in method <T#2,U,A,R>mapping(Function<? super T#2,? extends U>,Collector<? super U,A,R>)
1 error
Which I don't quite understand.

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

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

Post by admin »

Student::getName returns a String. This is the same return type that will be used to determine return type of the List returned by Collectors.toList() i.e. List<String>.

Therefore, the return type of Collectors.groupingBy will be Map<Integer, List<String>>.

But this is not compatible with the type of the variable grouping, which is Map<Integer, List<Student>>.
If you change the variable declaration to Map<Integer, List<String>> grouping, it will compile.

HTH,
Paul.

lenalena
Posts: 56
Joined: Tue Feb 21, 2017 4:24 pm
Contact:

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

Post by lenalena »

It's clear now, thank you. The exception given by the compiler actually makes sense.

It's a great explanation, perhaps you could add it to the question... (IMHO).

dongyingname
Posts: 18
Joined: Sat Jun 22, 2019 4:10 pm
Contact:

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

Post by dongyingname »

Code: Select all

Map<Integer, List<Student>> grouping = ls.stream().collect(Collectors.groupingBy(s ->s.getMarks()));
Shouldn't the above code return something like {20=[Student("S1", 20),Student("S3", 20)], 30=Student("S3", 30)} instead of {20=[S1:20, S3:20], 30=[S3:30]}?

The values of Map<Integer, List<Student>> are List<Student>s after all.

And shouldn't the answer be

Code: Select all

 Map<Integer, List<Student>> grouping = ls.stream().collect(Collectors.groupingBy(s ->s.getMarks(),Collectors.toList())); 
?

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

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

Post by admin »

Yes, the values are List<Student> but why do you think it will print Student("S1", 20), etc.?
Student implements toString method, and it prints - name+":"+marks;

Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests