Given the code fragment: List<String> nL = Arrays.asList("Jim", "John", "Jeff"); Function<String, String> funVal = s -> "Hello : ".contact(s); nL.Stream() .map(funVal) .peek(System.out::print); What is the result?
Click on the arrows to vote for the correct answer
A. B. C. D.C.
The given code fragment performs the following operations:
Arrays.asList()
method and assigns it to nL
.funVal
of type Function<String, String>
which takes a String argument and returns a new String which concatenates "Hello : " and the input String.stream()
method on the nL
list to get a Stream of its elements.map()
method on the Stream with the funVal
lambda expression as an argument. This transforms each String element of the Stream into a new String by applying the funVal
lambda expression to it.peek()
method on the Stream with a method reference to System.out::print
as an argument. This prints each element of the Stream to the console.The result of the code fragment is that each element of the nL
list is transformed into a new String with the funVal
lambda expression and printed to the console using the peek()
method. The output is:
Hello : Jim Hello : John Hello : Jeff
Therefore, the correct answer is A.