LearnFrenzy
Sign in Take a free test

HashMap and TreeMap · Predict the Output · Question 16

EnumMap keeps enum declaration order Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 16 Back to Exercise

What is the output of the following Java program?

Main.java
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links