- Most convenient way to convert enumeration values to string array in Java
- 2 Answers 2
- Original post
- A bit of generics
- Convert Enum list to comma separated String with matching Enum value
- 2 Answers 2
- Converting ArrayList of enums to String
- 3 Answers 3
- Polymorphically convert Java enum values into a list of strings
- 10 Answers 10
Most convenient way to convert enumeration values to string array in Java
And I want to convert this to a string array with minimal effort and no loops with Java 8+. This is the best I came up with:
Arrays.stream(Weather.values()).map(Enum::toString).toArray(String[]::new)
@christopher-schneider: I can. But that is not the point. My point is to create a minimal conversion, does not matter where it is.
2 Answers 2
Original post
Yes, that’s a good Java 8 way, but.
The toString can be overridden, so you’d better go with Weather::name which returns the name of an enum constant (exactly as declared in the enum declaration) and can’t be changed:
Stream.of(Weather.values()).map(Weather::name).toArray(String[]::new);
A bit of generics
I wrote a helper class to deal with any enum in a generic way:
class EnumUtils < public static > String[] getStringValues(Class enumClass) < return getStringValuesWithStringExtractor(enumClass, Enum::name); >public static > String[] getStringValuesWithStringExtractor( Class enumClass, Function extractor ) < return of(enumClass.getEnumConstants()).map(extractor).toArray(String[]::new); >>
enum Weather < RAINY, SUNNY, STORMY; @Override public String toString() < return String.valueOf(hashCode()); >public static void main(String[] args) < System.out.println(Arrays.toString(EnumUtils.getStringValues(Weather.class))); System.out.println(Arrays.toString(EnumUtils.getStringValuesWithStringExtractor(Weather.class, Weather::toString))); >>
[RAINY, SUNNY, STORMY] [359023572, 305808283, 2111991224]
Convert Enum list to comma separated String with matching Enum value
I have a List of Enum values like MON, TUE, WED, etc., same need to convert to comma-separated String. Need to use Java 8 to convert the same in an efficient way. For example.
Arrays.stream(Days.values()) .map(MON -> TimeRangeConstants.MON) .collect(Collectors.joining(","));
enum Days < MON, TUE, WED, THU, FRI, SAT, SUN; >main() < Days v1 = Days.MON; Days v2 = Days.WED; Days v3 = Days.FRI; Listdays = new ArrayList<>(); days.add(v1); days.add(v2); days.add(v3); String str = convertToString(days); > convertToString(List list) < // need to return String as "Monday, Wednesday, Friday" >
Because income request is having enum values and by framework converting to List
2 Answers 2
You would have to edit the enum to:
enum Days < MON("Monday"), TUE("Tuesday"), WED("Wednesday") ; private String val; Days(String val) < this.val = val; >@Override public String toString() < return val; >>
If you have access to the newer stream() method, you can do this:
final String s = String.join(",", list.stream().map(Object::toString).collect(Collectors.toList()); System.out.println("s mt24">)" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f4.0%2f" data-se-share-sheet-license-name="CC BY-SA 4.0" data-s-popover-placement="bottom-start">Share)" title="">Improve this answeranswered Aug 5, 2019 at 16:52 kailoonkailoon2,1291 gold badge18 silver badges33 bronze badges