How would I made this Map key autocomplete in java?

Viewed 44

I have this Typescript code that I want to make into Java. This is that code:

type CompTypes = "Texture" | "Layer" | "Sound";
let components: Map<CompTypes, Component>;

How would I do that in java?

1 Answers

You can define an enum for CompTypes:

public enum CompTypes { Texture, Layer, Sound };

And create a Component class with the structure you want.

Then, create an EnumMap with CompTypes as key and Component as value:

EnumMap<CompTypes, Component> enumMap = new EnumMap<CompTypes, Component>(CompTypes.class);

You can add a new pair:

enumMap.put(CompTypes.Layer, new Component());

And get an existing one:

enumMap.get(CompTypes.Layer)
Related