About Question enthuware.ocpjp.ii.v11.2.3325 :

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

Moderator: admin

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

About Question enthuware.ocpjp.ii.v11.2.3325 :

Post by dongyingname »

There is no correct answer, peek() only accept a Consumer as passed argument. getTitle() returns String not void, thus cannot be a Consumer.

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

Re: About Question enthuware.ocpjp.ii.v11.2.3325 :

Post by admin »

1. Did you try compiling and running it?
2. Recall from your OCA preparation that signature of a method does not include the return type.
If you like our products and services, please help us by posting your review here.

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

Re: About Question enthuware.ocpjp.ii.v11.2.3325 :

Post by dongyingname »

I understood now.
The compiler doesn't see the return type of the lambda expression
as long as I don't write

Code: Select all

long count = bkStrm.peek(x -> {return x.getTitle();}).count();//doesn't compile

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

Re: About Question enthuware.ocpjp.ii.v11.2.3325 :

Post by admin »

Well, bkStrm.peek(x -> {return x.getTitle();}).count(); would still be wrong and has nothing to do with the Consumer object that you are passing to the peek method. This statement doesn't compile because the method count() cannot be invoked on the type of the object returned by peek.
If you like our products and services, please help us by posting your review here.

daniko
Posts: 4
Joined: Sun Nov 10, 2019 3:20 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3325 :

Post by daniko »

What here could also be useful information, is that if the count can be calculated directly from the stream source, the intermediate operation won't be executed at all.

For example
List<String> l = Arrays.asList("A", "B", "C", "D");
long count = l.stream().peek(System.out::println).count();

The peek(System.out::println) won't be executed and the ABCD won't be printed because the count can be calculated directly from the variable l.

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

Re: About Question enthuware.ocpjp.ii.v11.2.3325 :

Post by admin »

Well, it is good information but what you have written is not entirely correct. As per the documentation, it is up to the implementation. It says,
"An implementation may choose to not execute the stream pipeline ....".
So, it is not a guarantee that the pipeline will not be executed.
If you like our products and services, please help us by posting your review here.

Badem48
Posts: 26
Joined: Thu Aug 24, 2023 4:33 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3325 :

Post by Badem48 »

Hi,

I have a question.

Code: Select all

        long count;
        long count2;
        var books = new ArrayList<Book>(List.of(new Book("The Outsider", "Stephen King"),
                new Book("Becoming", "Michelle Obama"), new Book("Uri", "India")));
        Stream bkStrm = books.stream();
        count = bkStrm.peek(x -> x.getTitle()).count(); //line 1
        count2 = books.stream().peek(x -> x.getTitle()).count(); //line 2
        System.out.println(count + " " + count2);
In this code the line //line 2 works fine but on //line 1, like in the question, does not work because it uses the raw type. Why it does not use the raw type on //line 2?

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

Re: About Question enthuware.ocpjp.ii.v11.2.3325 :

Post by admin »

Sorry, I did not understand your question. What error message are you getting?
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 36 guests