Here's one-liner for any enum
class:
public static String[] getNames(Class<? extends Enum<?>> e) {
return Arrays.stream(e.getEnumConstants()).map(Enum::name).toArray(String[]::new);
}
Pre Java 8 is still a one-liner, albeit less elegant:
public static String[] getNames(Class<? extends Enum<?>> e) {
return Arrays.toString(e.getEnumConstants()).replaceAll("^.|.$", "").split(", ");
}
That you would call like this:
String[] names = getNames(State.class); // any other enum class will work
If you just want something simple for a hard-coded enum class:
public static String[] names() {
return Arrays.toString(State.values()).replaceAll("^.|.$", "").split(", ");
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…