Which of the following fills in the blank so that the code outputs one line but uses a poor practice?
import java.util.*; public class Main { int count = 0; public void sneak(Collection<String> coll) { coll.stream(). ; }/*from w w w. j a v a2 s. com*/ public static void main(String[] args) { Main c = new Main(); c.sneak(Arrays.asList("asdf")); } }
findFirst()
findFirst()
findFirst()
D.
Option A is incorrect because it doesn't print out one line.
The peek()
method is an intermediate operation.
Since there is no terminal operation, the stream pipeline is not executed, so the peek()
method is never executed.
Options B and C are incorrect because they correctly output one line using a method reference and lambda, respectively, and don't use any bad practices.
Option D is the answer.
It does output one line.
However, it is bad practice to have a peek()
method that has side effects like modifying a variable.