如何创建一个键和值都为枚举的 EnumMap 对象?
how to create an EnumMap object with key and value both as Enum?
enum Month{JANUARY, FEBRUARY, MARCH, ...
}
enum Week{MONDAY, TUESDAY, WEDNESDAY, ...
}
Map<Month, String> monthMap = new EnumMap<>(Month.class);
可以像这样创建简单的 EnumMap,其中键是枚举,值是字符串
但是,我想创建 EnumMap,其中键和值都是枚举类型。
Map<Month, Week> monthWeekMap = new EnumMap<>
....
创建上述枚举映射对象的语法是什么。
语法没有什么不同:
Map<Month, Week> monthWeekMap = new EnumMap<>(Month.class);
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
内部值。
enum Month{JANUARY, FEBRUARY, MARCH, ...
}
enum Week{MONDAY, TUESDAY, WEDNESDAY, ...
}
Map<Month, String> monthMap = new EnumMap<>(Month.class);
可以像这样创建简单的 EnumMap,其中键是枚举,值是字符串
但是,我想创建 EnumMap,其中键和值都是枚举类型。
Map<Month, Week> monthWeekMap = new EnumMap<>
....
创建上述枚举映射对象的语法是什么。
语法没有什么不同:
Map<Month, Week> monthWeekMap = new EnumMap<>(Month.class);
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
内部值。