Question 16
Back to Exercise
What is the output of the following Java program?
import java.util.EnumMap;
import java.util.Map;
enum Size { S, M, L, XL }
public class Main {
public static void main(String[] args) {
Map<Size, Integer> orders = new EnumMap<>(Size.class);
for (String s : "L M XL M S L L".split(" ")) orders.merge(Size.valueOf(s), 1, Integer::sum);
System.out.println(orders);
}
}
- A {S=1, M=2, L=3, XL=1} Correct
- B {L=3, M=2, XL=1, S=1}
- C {L=3, M=2, S=1, XL=1}
- D {XL=1, L=3, M=2, S=1}
Correct Answer: Option A
EnumMap always iterates in the order the constants are declared.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!