1z0-829 Exam QuestionsBrowse all questions from this exam

1z0-829 Exam - Question 14


Given:

What is the result?

Show Answer
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

2 comments
Sign in to comment
james2033Option: D
Feb 15, 2024

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
Jun 26, 2024

Correct answer is D