美文网首页
枚举转化为list

枚举转化为list

作者: writeanewworld | 来源:发表于2020-07-20 17:09 被阅读0次

1.介绍

将枚举全部值转化成一个list

2.示例

public enum ChannelEnum {
    /**
     * 饿了么外卖
     */
    ELEME("eleme","饿了么外卖"),
    /**
     * 美团外卖
     */
    MTWM("mtwm","美团外卖"),
    /**
     * 京东外卖
     */
    JDWM("jdwm","京东外卖"),
    /**
     * 百度外卖
     */
    BDWM("bdwm","百度外卖");

    ChannelEnum(String channelCode, String channelName){
        this.channelCode = channelCode;
        this.channelName = channelName;
    }
    private String channelCode;
    private String channelName;

    public String getChannelCode() {
        return channelCode;
    }

    public void setChannelCode(String channelCode) {
        this.channelCode = channelCode;
    }

    public String getChannelName() {
        return channelName;
    }

    public void setChannelName(String channelName) {
        this.channelName = channelName;
    }

    /**
     * 将枚举值转化成list集合
     *
     * @return
     */
    public static List<String> toList(){
        List<String> list = new ArrayList<>();
        for (ChannelEnum channelEnum: ChannelEnum.values()
             ) {
                list.add(channelEnum.getChannelCode());
        }
        return list;
    }

}

获取枚举集合:

/**
 * 枚举集合
 */
private static List<String> list = ChannelEnum.toList();

3.例


image.png

相关文章

网友评论

      本文标题:枚举转化为list

      本文链接:https://www.haomeiwen.com/subject/nxyykktx.html