Problem
How to convert Enum to List of String in Java 8?
Solution
Using the Java 8 Streams, its map and collect functions we can convert Enum to List of Strings
Code
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class EnumToStringList {
public enum Alpha { A,B,C,D; }
public static void main(String[] args) {
List<String> strings = Stream.of(Alpha.values()).map(
Alpha::name).collect( Collectors.toList());
}
}
What if I've a list of Alpha with only values 'A' and 'B'
ReplyDeleteHow would you convert this list to a list of string ?