In java 8, count() acts as a terminal operation. so values will be printed. In java 9 or above, output might not be printed as the jre can determine the number from stream directly without using peek() operation on elements.
Please help, i typed the code as following but i'm not getting any output, what can be wrong?:
package birdie;
import java.util.Arrays;
import java.util.List;
class Vehicle {
public static void main(String[] args) {
List<Integer> values = Arrays.asList (1, 2, 3);
values.stream ()
.map(n -> n*2) //line n1
.peek(System.out::print) //line n2
.count();
}
}
The correct answer is B . when you use count() , you cant peak on the elements of the stream.
The number of elements covered by the stream source, a List, isknown and the intermediate operation, peek, does not inject intoor remove elements from the stream (as may be the case for flatMap or filter operations). Thus the count is thesize of the List and there is no need to execute the pipelineand, as a side-effect, print out the list elements.
no, Answer A is correct! copy paste the code into eclipse and be surprised ;-)
upvoted 2 times
...
...
This section is not available anymore. Please use the main Exam Page.1z0-809 Exam Questions
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
speed045
Highly Voted 4 years, 7 months agoRitesh_
Highly Voted 5 years, 8 months agoMPignaProTech
Most Recent 7 months, 2 weeks agosteefaand
1 year, 4 months agor1muka5
2 years, 4 months agokarta
2 years, 7 months agokishanrao
2 years, 10 months agoWilsonKKerll
3 years, 4 months agoYasinGaber
3 years, 4 months agoayzo
4 years, 5 months agorameasy
4 years, 11 months agoM_Jawad
5 years, 5 months agoPasci
5 years, 1 month ago