Exam 1z0-829 All QuestionsBrowse all questions from this exam
Question 14

Given:

What is the result?

    Correct Answer: D

    The provided code uses Java Streams to count the occurrences of each string in the list 'elements'. The code groups the list by each unique string and counts the number of times each string appears. Therefore, the result will be a map where keys are the unique strings and values are the counts of those strings. Given that the list contains three 'car', two 'truck', one 'bicycle', and one 'motorcycle', the correct output will be {bicycle=1, car=3, motorcycle=1, truck=2}.

Discussion
james2033Option: D

package q14; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; public class Test { public static void main(String[] args) { List<String> elements = Arrays.asList("car", "truck", "car", "bicycle", "car", "truck", "motorcycle"); Map<String, Long> outcome = elements.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); System.out.println(outcome); } } // Result: // {motorcycle=1, bicycle=1, car=3, truck=2}

xplorerpjOption: D

Correct answer is D