How to store value of enum in arraylist? How to store value of enum in arraylist? arrays arrays

How to store value of enum in arraylist?


You could build that list inside the enum itself like this:

public enum SomeEnum {    ENUM_VALUE1("Some value1"),    ENUM_VALUE2("Some value2"),    ENUM_VALUE3("Some value3");    private static final List<String> VALUES;    private final String value;    static {        VALUES = new ArrayList<>();        for (SomeEnum someEnum : SomeEnum.values()) {            VALUES.add(someEnum.value);        }    }    private SomeEnum(String value) {        this.value = value;    }    public static List<String> getValues() {        return Collections.unmodifiableList(VALUES);    }}

Then you can access this list with:

List<String> values = SomeEnum.getValues();


If you're using Java 8 and cannot change the enum:

List<String> list = Stream.of(SomeEnum.values())                          .map(SomeEnum::getValue)                          .collect(Collectors.toList());


You can simply create list from array like this:

List<String> list = Arrays.asList(SomeEnum.values());