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;
        }
        
    }
  • 相关阅读:
    316 Remove Duplicate Letters 去除重复字母
    315 Count of Smaller Numbers After Self 计算右侧小于当前元素的个数
    313 Super Ugly Number 超级丑数
    312 Burst Balloons 戳气球
    309 Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
    Java 类成员的初始化顺序
    JavaScript 全局
    HTML字符实体
    Java中的toString()方法
    JavaScript 弹窗
  • 原文地址:https://www.cnblogs.com/skyesx/p/4417811.html
Copyright © 2011-2022 走看看