如何在arraylist中存储枚举值?

时间:2023-01-26 14:28:57

I have an enum like this:

我有这样的枚举:

public enum SomeEnum 
{
    ENUM_VALUE1("Some value1"),
    ENUM_VALUE2("Some value2"),
    ENUM_VALUE3("Some value3");
}

I need to store values of enum Some value1, Some value2 and Some value3 in an ArrayList.

我需要存储enum的值在ArrayList中存储一些value1,some value2和Some3。

I can get all values in an array using SomeEnum.values() and iterate over that array and store the value in an ArrayList like this:

我可以使用SomeEnum.values()获取数组中的所有值,并迭代该数组并将值存储在ArrayList中,如下所示:

SomeEnum values[] = SomeEnum.values();
ArrayList<SomeEnum> someEnumArrayList = new ArrayList<SomeEnum>();
for(SomeEnum value:values) 
{
    someEnumArrayList.add(value.getValue());
}

Is there any other method like values() that returns array of Some value1, Some value2 and Some value3?

是否有任何其他方法,如values()返回某些value1的数组,一些value2和一些value3?

3 个解决方案

#1


9  

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();

#2


4  

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

如果您使用的是Java 8且无法更改枚举:

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

#3


2  

You can simply create list from array like this:

你可以简单地从数组创建列表,如下所示:

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

#1


9  

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();

#2


4  

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

如果您使用的是Java 8且无法更改枚举:

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

#3


2  

You can simply create list from array like this:

你可以简单地从数组创建列表,如下所示:

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