0
enum Month{JANUARY, FEBRUARY, MARCH, ...
}

enum Week{MONDAY, TUESDAY, WEDNESDAY, ...
}

Map<Month, String> monthMap = new EnumMap<>(Month.class); Simple EnumMap can be created like this where key is Enum and value is String

However, I want to create the EnumMap where key and value both are of enum type.

Map<Month, Week> monthWeekMap = new EnumMap<> ....
what will be the syntax for creating the above enum map objet.

0

1 Answer 1

5

The syntax is no different:

Map<Month, Week> monthWeekMap = new EnumMap<>(Month.class);

The EnumMap constructor only needs the Class of the key type, in order to decide how to allocate the array used for the actual storage (an EnumMap is effectively just a strongly-typed array, whose length is the number of elements in the enum: this is obtained by reflection); the value type is essentially irrelevant, because it's just storing Object values internally.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.