About Question enthuware.ocpjp.v8.2.1899 :

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:

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

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

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

foxxbl
Posts: 4
Joined: Tue Nov 29, 2016 12:32 pm
Location: Dublin,Ireland
Contact:

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

Post 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())

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

MoistCarrot
Posts: 6
Joined: Fri Oct 26, 2018 9:18 pm
Contact:

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

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

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

henrid
Posts: 31
Joined: Sun Oct 13, 2013 1:20 pm
Contact:

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

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

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

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

Post 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".
If you like our products and services, please help us by posting your review here.

henrid
Posts: 31
Joined: Sun Oct 13, 2013 1:20 pm
Contact:

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

Post 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 ;)

vleunti
Posts: 4
Joined: Tue Aug 25, 2020 3:50 am
Contact:

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

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

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

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

Post by admin »

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.

vleunti
Posts: 4
Joined: Tue Aug 25, 2020 3:50 am
Contact:

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

Post 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]}}
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

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

vleunti
Posts: 4
Joined: Tue Aug 25, 2020 3:50 am
Contact:

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

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

Post Reply

Who is online

Users browsing this forum: No registered users and 48 guests