3

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;
   List<Days> days = new ArrayList<>();
   days.add(v1);
   days.add(v2);
   days.add(v3);
    String str = convertToString(days);
}


convertToString(List<Days> list) {      
   // need to return String as "Monday, Wednesday, Friday" 
}

For the given above example, I need output as "Monday, Wednesday, Friday"

3
  • 1
    Why not use the DayOfWeek enum that already exists in the standard API? Commented Aug 5, 2019 at 16:36
  • I don't understand your example. Map everything to Monday...? Commented Aug 5, 2019 at 16:42
  • Because income request is having enum values and by framework converting to List<Days> which I don't have control on that to change. But in the service layer, I need to change List<Days> into String with comma separated values. Commented Aug 5, 2019 at 16:47

2 Answers 2

3

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 = " + s);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, kailoon :) got my solution
@kailoon's answer helped me out too! thanks! my IDE showed me you can avoid using String.join by collecting with ...collect(Collectors.joining(",")), in context of the whole stream that looks like this: stream(Days).map(Objects::toString).collect(Collectors.joining(","))
1

You can declare a new method in the enum to map the day to the name of the day and then use the java-8 streams like this:

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class DaysToCsv {

    enum Days {
        MON, TUE, WED, THU, FRI, SAT, SUN;

        public static String getFullName(Days day) {
            switch (day) {
                case MON:
                    return "Monday";
                case TUE:
                    return "Tuesday";
                case WED:
                    return "Wednesday";
                case THU:
                    return "Thursday";
                case FRI:
                    return "Friday";
                case SAT:
                    return "Saturday";
                case SUN:
                    return "Sunday";
                default:
                    throw new IllegalArgumentException("Unexpected day");
            }
        }
    }

    public static void main(String[] args) {
        Days v1 = Days.MON;
        Days v2 = Days.WED;
        Days v3 = Days.FRI;
        List<Days> days = new ArrayList<>();
        days.add(v1);
        days.add(v2);
        days.add(v3);
        String str = convertToString(days);
        System.out.println(str);
    }

    public static String convertToString(List<Days> list) {
        return list.stream().map(day -> Days.getFullName(day)).collect(Collectors.joining(", "));
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.