zoukankan      html  css  js  c++  java
  • JAVABEAN递归转MAP实现

    之前想找这么一个方法,找到的都不是递归实现的,列表、MAP里面的都没转,就自己折腾了个。——YOYO

    public class ObjectToMap{
        
        public static Map objectToMap(Object obj){
            try{
                Class type = obj.getClass();
                Map returnMap = new HashMap();
                BeanInfo beanInfo = Introspector.getBeanInfo(type);
    
                PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();
                for (int i = 0; i< propertyDescriptors.length; i++) {
                    PropertyDescriptor descriptor = propertyDescriptors[i];
                    String propertyName = descriptor.getName();
                    if (!propertyName.equals("class")) {
                        Method readMethod = descriptor.getReadMethod();
                        Object result = readMethod.invoke(obj, new Object[0]);
                        if(result == null){
                            continue;
                        }
                        //判断是否为 基础类型 String,Boolean,Byte,Short,Integer,Long,Float,Double
                        //判断是否集合类,COLLECTION,MAP              
                        if(result instanceof String 
                                || result instanceof Boolean 
                                || result instanceof Byte 
                                || result instanceof Short 
                                || result instanceof Integer 
                                || result instanceof Long 
                                || result instanceof Float 
                                || result instanceof Double 
                                || result instanceof Enum 
                                ){
                            if (result != null) {
                                returnMap.put(propertyName, result);
                            }
                        }else if(result instanceof Collection){                        
                            Collection<?> lstObj = arrayToMap((Collection<?>)result);
                            returnMap.put(propertyName, lstObj);
                                    
                        }else if(result instanceof Map){
                            Map<Object,Object> lstObj = mapToMap((Map<Object,Object>)result);
                            returnMap.put(propertyName, lstObj);
                        } else {
                            Map mapResult = objectToMap(result);
                            returnMap.put(propertyName, mapResult);
                        }
                        
                    }
                }
                return returnMap;
            }catch(Exception e){
                throw new RuntimeException(e);
            }
            
        }    
        
        private static Map<Object, Object> mapToMap(Map<Object, Object> orignMap) {
            Map<Object,Object> resultMap = new HashMap<Object,Object>();
            for(Entry<Object, Object> entry:orignMap.entrySet()){
                Object key = entry.getKey();
                Object resultKey = null;
                if(key instanceof Collection){
                    resultKey = arrayToMap((Collection)key);
                }else if(key instanceof Map){
                    resultKey = mapToMap((Map)key);
                }
                else{
                    if(key instanceof String 
                            || key instanceof Boolean 
                            || key instanceof Byte 
                            || key instanceof Short 
                            || key instanceof Integer 
                            || key instanceof Long 
                            || key instanceof Float 
                            || key instanceof Double 
                            || key instanceof Enum 
                            ){
                        if (key != null) {
                            resultKey = key;
                        }
                    }else{
                        resultKey = objectToMap(key);
                    }                
                }
                
    
                Object value = entry.getValue();
                Object resultValue = null;
                if(value instanceof Collection){
                    resultValue = arrayToMap((Collection)value);
                }else if(value instanceof Map){
                    resultValue = mapToMap((Map)value);
                }
                else{
                    if(value instanceof String 
                            || value instanceof Boolean 
                            || value instanceof Byte 
                            || value instanceof Short 
                            || value instanceof Integer 
                            || value instanceof Long 
                            || value instanceof Float 
                            || value instanceof Double 
                            || value instanceof Enum 
                            ){
                        if (value != null) {
                            resultValue = value;
                        }
                    }else{
                        resultValue = objectToMap(value);
                    }                
                }
                
                resultMap.put(resultKey, resultValue);
            }        
            return resultMap;
        }
    
    
        private static Collection arrayToMap(Collection lstObj){
            ArrayList arrayList = new ArrayList();
            
            for (Object t : lstObj) {
                if(t instanceof Collection){
                    Collection result = arrayToMap((Collection)t);
                    arrayList.add(result);
                }else if(t instanceof Map){
                    Map result = mapToMap((Map)t);
                    arrayList.add(result);
                } else {
                    if(t instanceof String 
                            || t instanceof Boolean 
                            || t instanceof Byte 
                            || t instanceof Short 
                            || t instanceof Integer 
                            || t instanceof Long 
                            || t instanceof Float 
                            || t instanceof Double 
                            || t instanceof Enum 
                            ){
                        if (t != null) {
                            arrayList.add(t);
                        }
                    }else{
                        Object result = objectToMap(t);
                        arrayList.add(result);    
                    }                
                }
            }
            return arrayList;
        }
        
    }
  • 相关阅读:
    Mac配置docker阿里云加速器
    Docker初学笔记
    Mac下载安装Tomcat
    MySQL
    monkey
    Git基本使用
    yaml语法
    PAT_B数素数 (20)
    PAT_B1002数字分类
    PAT基础编程练习
  • 原文地址:https://www.cnblogs.com/skyesx/p/4417811.html
Copyright © 2011-2022 走看看