I have an enum
with another enum
as a parameter
public enum MyEntity{
Entity1(EntityType.type1,
....
MyEntity(EntityType ty
I guess there are some typos in your code (the method should be static in my opinion, your constructor is doing a no-op at the moment), but if I'm following you, you can create a stream from the array of enums and use the toMap
collector, mapping each enum with its EntityType
for the keys, and mapping the instance itself as a value:
private static final Map lookup =
Arrays.stream(EntityTypeInfo.values())
.collect(Collectors.toMap(EntityTypeInfo::getEntityType, e -> e));
The toMap
collector does not make any guarantee about the map implementation returned (although it's currently a HashMap
), but you can always use the overloaded variant if you need more control, providing a throwing merger as parameter.
You could also use another trick with a static class, and fill the map in the constructor.