Page 1 of 1
Re: About Question enthuware.ocpjp.v8.2.1899 :
Posted: Tue May 16, 2017 11:37 pm
by lenalena
Thanks!
Also, please, confirm that the reason option 5 is incorrect is because Book::getAuthor will return a String, which will become the type of List returned by Collectors.toList, and it will be incompatible with expected List<Book>...
Re: About Question enthuware.ocpjp.v8.2.1899 :
Posted: Tue May 16, 2017 11:47 pm
by admin
lenalena wrote:Thanks!
Also, please, confirm that the reason option 5 is incorrect is because Book::getAuthor will return a String, which will become the type of List returned by Collectors.toList, and it will be incompatible with expected List<Book>...
That is correct.
Re: About Question enthuware.ocpjp.v8.2.1899 :
Posted: Sun Jun 25, 2017 5:01 pm
by foxxbl
Just to add, to provide compilable EXPRESSION for the Option 5, toList method requires parenthesis () :
Book::getGenre, Collectors.mapping(Book::getAuthor, Collectors.toList())
Re: About Question enthuware.ocpjp.v8.2.1899 :
Posted: Sun Jun 25, 2017 11:06 pm
by admin
It will not compile even with the (). It is a wrong option anyway. The explanation tries to show you how to work out Collectors.groupingBy logically.
Re: About Question enthuware.ocpjp.v8.2.1899 :
Posted: Fri Oct 26, 2018 9:22 pm
by MoistCarrot
I am confused as to where the List<Book> is coming from in the "classified" Map. The functions in Collectors.groupingBy are both returning strings. The explanation listed does not really help me because it is just providing an example rather than explaining what each operation is doing to satisfy the requirements of the "classified" Map.
Could someone please explain what is going on here? Thanks.
Re: About Question enthuware.ocpjp.v8.2.1899 :
Posted: Fri Oct 26, 2018 9:54 pm
by admin
List<Book> is coming from Collectors.groupingBy(Book::getAuthor).
The Collectors functionality has a steep learning curve but it is easy once you get the hang of it. You will need to go through a book or tutorial and write a few test program to really get it. IMHO, it is nearly impossible to understand this topic without writing some code.
HTH,
Paul.
Re: About Question enthuware.ocpjp.v8.2.1899 :
Posted: Tue Sep 10, 2019 10:54 am
by henrid
I think Collectors.groupingBy(Book::getAuthor) is a shortcut for Collectors.groupingBy(Book::getAuthor,Collections.toList()) and that's where the List<Book> is coming from, right?
Re: About Question enthuware.ocpjp.v8.2.1899 :
Posted: Tue Sep 10, 2019 10:53 pm
by admin
No, Collectors.groupingBy(Book::getAuthor) returns a Collector that classifies incoming stream objects into a map where the key is returned by Book::getAuthor and the value is a list containing the Book object that has the same author.
Let's says you get a Book object from the stream, you call getAuthor on it and you get "Author1". Therefore, this Book object will go into the list of Books whose author is "Author1".
Re: About Question enthuware.ocpjp.v8.2.1899 :
Posted: Fri Sep 13, 2019 9:18 am
by henrid
I tried it out, and Collectors.groupingBy(Function) gives the same Map output as Collectors.groupingBy(Function,Collectors.toList())
So for me it is a shortcut, even if you don't want to call it that way

Re: About Question enthuware.ocpjp.v8.2.1899 :
Posted: Tue Aug 25, 2020 3:54 am
by vleunti
Hi, why i become this result?
{action={James=[enthuware.Test5@17b9294, enthuware.Test5@e8cba497], Ion=[enthuware.Test5@59d2979c]}}
action = genre
James = author
[enthuware.Test5@17b9294, enthuware.Test5@e8cba497] ??? these are objects , but should to be books titles...
This is my code example:
Map<String, Map<String, List<Test5>>> classified = null;
classified = test.stream().collect(Collectors.groupingBy(Test5::getGenre, Collectors.groupingBy(Test5::getAuthor)));
Thanks.
Re: About Question enthuware.ocpjp.v8.2.1899 :
Posted: Tue Aug 25, 2020 4:49 am
by admin
Please post exact and complete code that you are executing.
Re: About Question enthuware.ocpjp.v8.2.1899 :
Posted: Tue Aug 25, 2020 5:00 am
by vleunti
admin wrote: ↑Tue Aug 25, 2020 4:49 am
Please post exact and complete code that you are executing.
Code: Select all
public class Test5 {
private String title;
private String genre;
private String author;
public Test5(String title, String genre, String author) {
this.title = title;
this.genre = genre;
this.author = author;
}
public String getTitle() {
return title;
}
public String getGenre() {
return genre;
}
public String getAuthor() {
return author;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Test5> test = Arrays.asList(
new Test5("There is nothing to do", "action", "James"),
new Test5("Java ist auch ein Insel", "action", "James"),
new Test5("Berlin ist wunderschon", "action", "John")
);
Map<String, Map<String, List<Test5>>> classified = null;
classified = test.stream().collect(Collectors.groupingBy(Test5::getGenre, Collectors.groupingBy(Test5::getAuthor)));
System.out.println(classified);
}
}
Output: {action={James=[enthuware.Test5@214c265e, enthuware.Test5@448139f0], John=[enthuware.Test5@7cca494b]}}
Re: About Question enthuware.ocpjp.v8.2.1899 :
Posted: Tue Aug 25, 2020 6:02 am
by admin
You are grouping Test5 instances by author when you do Collectors.groupingBy(Test5::getAuthor). So, the resulting map will contain author as keys and Test5 instances as values.
Re: About Question enthuware.ocpjp.v8.2.1899 :
Posted: Tue Aug 25, 2020 6:44 am
by vleunti
admin wrote: ↑Tue Aug 25, 2020 6:02 am
You are grouping Test5 instances by author when you do Collectors.groupingBy(Test5::getAuthor). So, the resulting map will contain author as keys and Test5 instances as values.
Thank you, i thought a little bit and i understood what i've had to do. Now i have my books titles.
Code: Select all
classified = test.stream().collect(Collectors.groupingBy(Test5::getGenre,Collectors.groupingBy(Test5::getAuthor,Collectors.mapping(Test5::getTitle, Collectors.toList()))));
Output: {action={James=[There is nothing to do, Java ist auch ein Insel], John=[Berlin ist wunderschon]}}
so,it works now.