About Question enthuware.ocpjp.ii.v11.2.3325 :
Moderator: admin
-
- Posts: 18
- Joined: Sat Jun 22, 2019 4:10 pm
- Contact:
About Question enthuware.ocpjp.ii.v11.2.3325 :
There is no correct answer, peek() only accept a Consumer as passed argument. getTitle() returns String not void, thus cannot be a Consumer.
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3325 :
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.
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.
-
- Posts: 18
- Joined: Sat Jun 22, 2019 4:10 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3325 :
I understood now.
The compiler doesn't see the return type of the lambda expression
as long as I don't write
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
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3325 :
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.
-
- Posts: 4
- Joined: Sun Nov 10, 2019 3:20 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3325 :
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.
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.
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3325 :
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.
"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.
-
- Posts: 26
- Joined: Thu Aug 24, 2023 4:33 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3325 :
Hi,
I have a question.
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?
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);
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3325 :
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.
Who is online
Users browsing this forum: No registered users and 24 guests