Given the code fragment:
List
values.stream ()
.map(n -> n*2) //line n1
.peek(System.out::print) //line n2
.count();
What is the result?
Given the code fragment:
List
values.stream ()
.map(n -> n*2) //line n1
.peek(System.out::print) //line n2
.count();
What is the result?
The code produces no output. The count() method is a terminal operation that returns the number of elements in the stream. The peek() method is meant to support debugging by performing the action provided on each element as elements are consumed from the resulting stream. However, in this case, since count() is used and it does not consume the stream elements in a way that executes peek(), the peek() operation does not produce any output.
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.
Answer is A
Ans: A
Yes the correct answer is 246, so A is the correct option.
In java 8 answer is A In java 9 or more answer is B
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 ;-)
Answer is A. Each element is doubled and printed with peek operation, while result of count operation is ignored.
The correct answer is A.
The answer is B, output might not be printed
A test.
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(); } }