java 读取枚举类的值转成list和map

首先Enum类:

public enum RuleTypeEnum {  
  //officeClerk,estimator,administrator,personnel都是国际化的key值
    officeClerk("officeClerk",99),  
    estimator("estimator",1),     
    administrator("administrator",2), 
    personnel("personnel",3);      

   private String reason;
    private int index;

    RuleTypeEnum(String reason, int index) {
        this.reason = reason;
        this.index = index;
    }


    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    } 
}

/下面为Enum工具类/

public class EnumUtils {
  private static Logger log= LoggerFactory.getLogger(EnumUtils.class); 
    /**
     *  把枚举类的值都转成Map
     * @param enumT 枚举类
     * @param methodNames 读取的方法
     * @param <T>
     * @return
     */
    public static <T> Map<String, String> EnumToMap(Class<T> enumT,String... methodNames) {
        Map<String, String> enummap = new HashMap<String, String>();
        if (!enumT.isEnum()) { //如果不是enum
            return enummap;
        }
        T enums []= enumT.getEnumConstants(); //得到枚举类里的实例
        if (enums == null || enums.length <= 0) {
            return enummap;
        }
        int count = methodNames.length;
        /**默认接口key方法*/
        String keyMathod = "getReason";
        /**默认接口value方法*/
        String valueMathod = "getIndex";
         
        if (count >= 1 && !"".equals(methodNames[0])) {
            keyMathod = methodNames[0];
        }
        if (count == 2 && !"".equals(methodNames[1])) {
            valueMathod = methodNames[1];
        }
        for (int i = 0;i < enums.length; i++) {
            T tobj = enums[i];
            try {
                /**获取key值*/
                Object resultkey = getMethodValue(keyMathod, tobj);
                if ("".equals(resultkey)) {
                    continue;
                }
                /**获取value值*/
                Object resultValue = getMethodValue(valueMathod, tobj);
                /**如果描述不存在获取整条enum*/
                if ("".equals(resultValue)) {
                    resultValue = tobj;
                }
                enummap.put(resultkey+"", resultValue+"");
            } catch (Exception e) {
                e.printStackTrace();
                log.error("枚举类转成Map异常",e);
            }
        }
        return enummap;
    }

    private static <T> Object getMethodValue(String methodName, T obj, Object... args) {
        Object resut = "";
        try {
            /********************************* start *****************************************/
            /**获取方法数组,这里只要公有的方法*/
            Method[] methods = obj.getClass().getMethods();
            if (methods.length <= 0) {
                return resut;
            }
            Method method = null;
            for (int i = 0, len = methods.length; i < len; i++) {
                /**忽略大小写取方法*/
                if (methods[i].getName().equalsIgnoreCase(methodName)) {
                    /**如果存在,则取出正确的方法名称*/
                    methodName = methods[i].getName();
                    method = methods[i];
                    break;
                }
            }
            /*************************** end ***********************************************/
            if (method == null) {
                return resut;
            }
            /**方法执行*/
            resut = method.invoke(obj, args);
            if (resut == null) {
                resut = "";
            }
            /**返回结果*/
            return resut;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resut;
    }


    /***
     *  取枚举的key 和 value 返回list
     * @param enumT 枚举类
     * @param methodNames 取枚举类的方法
     * @param <T>
     * @return
     */
    public static <T> List<EnumEntity> getEnumToList(Class<T> enumT, String... methodNames) {
        List<EnumEntity> enumList = new ArrayList<>(); //最终返回的list  enumEntity是对枚举类的封装实体
        if (!enumT.isEnum()) {
            return enumList;
        }
        T enums[] = enumT.getEnumConstants();  //得么枚举类里所有的枚举类
        if (enums.length == 0) {  //如果没有枚举键和值   结束
            return enumList;
        }
        int count = methodNames.length;
        String keyMethod = "getReason";  //默认的取 key的方法
        String valueMethod = "getIndex";  //默认的取 value 的方法
        if (count >= 1 && !methodNames[0].equals("")) { //如果方法的长度是大于等于1的,并且不为空
            keyMethod = methodNames[0];
        }
        if (count == 2 && !methodNames[1].equals("")) { //如果方法的长度是等于2的,并且不为空
            valueMethod = methodNames[1];
        }
        try {
            for (int i = 0; i < enums.length; i++) {
                EnumEntity enumEntity = new EnumEntity();
                T object = enums[i];     //得到枚举类里每条值
                Object resultKey = getMethodValue(keyMethod, object); //获取key值
                if (resultKey.equals("")) {
                    continue;
                }
                Object resultValue = getMethodValue(valueMethod, object); //获取value值
                if (resultValue.equals("")) {
                    resultValue = object;
                }
                //MessageUtils.getMessage为读取国际化的.
                enumEntity.setKey(MessageUtils.getMessage(resultKey.toString()));  //把key存到实体类
                enumEntity.setValue(resultValue.toString()); //把value存到实体类
                enumList.add(enumEntity);   //存到list
            }
        } catch (Exception e) {
            e.getStackTrace();
            log.error("枚举类转成List异常", e);
        }
        return enumList;
    }
}

//下面是调用的方法

public List<EnumEntity> getRuleType() { Class<RuleTypeEnum> clasz= RuleTypeEnum.class; List<EnumEntity> list= EnumUtils.getEnumToList(clasz); //用默认的方法名 getReason和getIndex return list; }

/完美//

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值