About Question enthuware.ocpjp.v8.2.1899 :
Moderator: admin
-
- Posts: 56
- Joined: Tue Feb 21, 2017 4:24 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1899 :
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>...
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>...
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1899 :
That is correct.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>...
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Tue Nov 29, 2016 12:32 pm
- Location: Dublin,Ireland
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1899 :
Just to add, to provide compilable EXPRESSION for the Option 5, toList method requires parenthesis () :
Book::getGenre, Collectors.mapping(Book::getAuthor, Collectors.toList())
Book::getGenre, Collectors.mapping(Book::getAuthor, Collectors.toList())
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1899 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Fri Oct 26, 2018 9:18 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1899 :
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.
Could someone please explain what is going on here? Thanks.
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1899 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 31
- Joined: Sun Oct 13, 2013 1:20 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1899 :
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?
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1899 :
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".
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".
If you like our products and services, please help us by posting your review here.
-
- Posts: 31
- Joined: Sun Oct 13, 2013 1:20 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1899 :
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
So for me it is a shortcut, even if you don't want to call it that way
-
- Posts: 4
- Joined: Tue Aug 25, 2020 3:50 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1899 :
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.
{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.
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1899 :
Please post exact and complete code that you are executing.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Tue Aug 25, 2020 3:50 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1899 :
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);
}
}
Last edited by admin on Tue Aug 25, 2020 5:59 am, edited 1 time in total.
Reason: Added [code] [/code] tags around the code
Reason: Added [code] [/code] tags around the code
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1899 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Tue Aug 25, 2020 3:50 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1899 :
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()))));
so,it works now.
Who is online
Users browsing this forum: No registered users and 130 guests