zoukankan      html  css  js  c++  java
  • 通用java枚举类转List-Map集合

    java的枚举类功能是很强大的,在平时开发中也用的比较多,有时候可能会有将枚举类转成List集合这种需求,如果能确定枚举类中的字段固定可以使用实体类接收,如果不固定的话,很多时候只能用Map来接收了。

    这里实现一下java枚举类转List-Map集合:

    	/**
         * java枚举类转List<Map>集合
         * 
         * @param clazz
         * @return null-该class不是枚举类型  []-该枚举类型没有自定义字段  list-获取该枚举类型的List<Map>返回结果
         */
        public static List<Map<String, Object>> enumToListMap(Class<?> clazz) {
            List<Map<String, Object>> resultList = null;
            // 判断是否是枚举类型
            if ("java.lang.Enum".equals(clazz.getSuperclass().getCanonicalName())) {
                resultList = new ArrayList<>();
                // 获取所有public方法
                Method[] methods = clazz.getMethods();
                List<Field> fieldList = new ArrayList<>();
                for (int i = 0; i < methods.length; i++) {
                    String methodName = methods[i].getName();
                    if (methodName.startsWith("get") && !"getDeclaringClass".equals(methodName)
                        && !"getClass".equals(methodName)) { // 找到枚举类中的以get开头的(并且不是父类已定义的方法)所有方法
                        Field field = null;
                        try {
                            field = clazz.getDeclaredField(StringUtils.uncapitalize(methodName.substring(3))); // 通过方法名获取自定义字段
                        } catch (NoSuchFieldException | SecurityException e) {
                            e.printStackTrace();
                        }
                        if (field != null) { // 如果不为空则添加到fieldList集合中
                            fieldList.add(field);
                        }
                    }
                }
                if (!fieldList.isEmpty()) { // 判断fieldList集合是否为空
                    Map<String, Object> map = null;
                    Enum<?>[] enums = (Enum[])clazz.getEnumConstants(); // 获取所有枚举
                    for (int i = 0; i < enums.length; i++) {
                        map = new HashMap<>();
                        for (int l = 0, len = fieldList.size(); l < len; l++) {
                            Field field = fieldList.get(l);
                            field.setAccessible(true);
                            try {
                                map.put(field.getName(), field.get(enums[i])); // 向map集合添加字段名称 和 字段值
                            } catch (IllegalArgumentException | IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        }
                        resultList.add(map);// 将Map添加到集合中
                    }
                }
            }
            return resultList;
        }
    

    新建几个枚举类和普通类测试一下:

    public class TestEnum0 {
    
    }
    
    public enum TestEnum1 {
        TestEnum1;
    }
    
    public enum TestEnum2 {
        TestEnum2("TestEnum2");
    
        private String name;
    
        TestEnum2(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    }
    
    public enum TestEnum3 {
        TestEnum3(1, "TestEnum3", "测试枚举类");
    
        private int id;
    
        private String message;
    
        private String desc;
    
        TestEnum3(int id, String message, String desc) {
            this.id = id;
            this.message = message;
            this.desc = desc;
        }
    
        public int getId() {
            return id;
        }
    
        public String getMessage() {
            return message;
        }
    
        public String getDesc() {
            return desc;
        }
    }
    

    编写测试方法:

    	@Test
        public void enumToListMapTest() {
            // 普通类
            System.out.println(enumToListMap(TestEnum0.class));
            // 枚举类
            System.out.println(enumToListMap(TestEnum1.class));
            System.out.println(enumToListMap(TestEnum2.class));
            System.out.println(enumToListMap(TestEnum3.class));
        }
    

    运行结果:

    null
    []
    [{name=TestEnum2}]
    [{id=1, message=TestEnum3, desc=测试枚举类}]
    
    一颗安安静静的小韭菜。文中如果有什么错误,欢迎指出。
  • 相关阅读:
    SuperMap房产测绘成果管理平台
    SuperMap产权登记管理平台
    Android adb shell am 的用法(1)
    由浅入深谈Perl中的排序
    Android 内存监测和分析工具
    Android 网络通信
    adb server is out of date. killing...
    引导页使用ViewPager遇到OutofMemoryError的解决方案
    adb logcat 详解
    How to send mail by java mail in Android uiautomator testing?
  • 原文地址:https://www.cnblogs.com/c-Ajing/p/13448335.html
Copyright © 2011-2022 走看看