Get the enumeration value as a string list in Java 8
•
Java
Is there a Java 8 method or a simple method that returns the enumeration value as a string list, such as:
List<String> sEnum = getEnumValuesAsString();
Solution
You can do (before Java 8):
List<Enum> enumValues = Arrays.asList(Enum.values());
or
List<Enum> enumValues = new ArrayList<Enum>(EnumSet.allOf(Enum.class));
Using java 8 functionality, you can map each constant to its name:
List<String> enumNames = Stream.of(Enum.values()) .map(Enum::name) .collect(Collectors.toList());
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码