枚举的简单使用

枚举的简单使用如下:

                根据code获取name             

public static String getName(Integer code){
        for(TestEunm testEunm : TestEunm.values()){
            if(testEunm.code.equals(code)){
                return testEunm.name;
            }
        }
        return null;
    }

                获取全部枚举:

        

public static List<CommonEunm> getList(){
        List<CommonEunm> eunmList = Lists.newArrayList();
        for (TestEunm testEunm : TestEunm.values()){
            //创建一个有name和code的类
            CommonEunm commonEunm = new CommonEunm();
            commonEunm.setCode(testEunm.code);
            commonEunm.setName(testEunm.name);
            eunmList.add(commonEunm);
        }
        return eunmList;
    }

完整代码:

        

package com.info.eunmUtils;

import com.info.Vo.CommonEunm;
import org.assertj.core.util.Lists;
import java.util.List;

public enum TestEunm {
    ONES("第一",10),
    TWOS("第二",20),
    THREES("第三",30),
    FOURS("第四",40);

    private Integer code;
    private String name;

    public Integer getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    TestEunm(String name, Integer code) {
        this.code = code;
        this.name = name;
    }

    /**
     *根据code查找name(根据name查找code同理)
     * @return
     */
    public static String getName(Integer code){
        for(TestEunm testEunm : TestEunm.values()){
            if(testEunm.code.equals(code)){
                return testEunm.name;
            }
        }
        return null;
    }

    /**
     * 获取全部枚举
     */
    public static List<CommonEunm> getList(){
        List<CommonEunm> eunmList = Lists.newArrayList();
        for (TestEunm testEunm : TestEunm.values()){
            //创建一个有name和code的类
            CommonEunm commonEunm = new CommonEunm();
            commonEunm.setCode(testEunm.code);
            commonEunm.setName(testEunm.name);
            eunmList.add(commonEunm);
        }
        return eunmList;
    }



}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值