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
...
...
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, 1 month agoRitesh_
Highly Voted 5 years, 3 months agoMPignaProTech
Most Recent 1 month, 4 weeks agosteefaand
11 months, 1 week agor1muka5
1 year, 10 months agokarta
2 years, 1 month agokishanrao
2 years, 5 months agoWilsonKKerll
2 years, 10 months agoYasinGaber
2 years, 11 months agoayzo
3 years, 11 months agorameasy
4 years, 5 months agoM_Jawad
4 years, 12 months agoPasci
4 years, 7 months ago