11分钟掌握Java对象和Map转换


11分钟掌握Java对象和Map转换

或许前路永夜,即便如此我也要前进,因为星光即使微弱也会为我照亮前路


1. 将Javabean对象装换为map

        /**
         * @param bean
         * @return
         */
        public static <T> Map<String, Object> beanToMap(T bean) {
            Map<String, Object> map = Maps.newHashMap();
            if (bean != null) {
                BeanMap beanMap = BeanMap.create(bean);
                for (Object key : beanMap.keySet()) {
                    map.put(key + "", beanMap.get(key));
                }
            }
            return map;
        }

2.将map装换为Javabean对象

        /**
         * @param map
         * @param bean
         * @return
         */
        public static <T> T mapToBean(Map<String, Object> map, T bean) {
            BeanMap beanMap = BeanMap.create(bean);
            beanMap.putAll(map);
            return bean;
        }

3.将List转换为List<Map<String, Object>>

        /**
         * @param objList
         * @return
         * @throws JsonGenerationException
         * @throws JsonMappingException
         * @throws IOException
         */
        public static <T> List<Map<String, Object>> objectsToMaps(List<T> objList) {
            List<Map<String, Object>> list = Lists.newArrayList();
            if (objList != null && objList.size() > 0) {
                Map<String, Object> map = null;
                T bean = null;
                for (int i = 0, size = objList.size(); i < size; i++) {
                    bean = objList.get(i);
                    map = beanToMap(bean);
                    list.add(map);
                }
            }
            return list;
        }

4. 将List<Map<String,Object>>转换为List

    /**
         * @param maps
         * @param clazz
         * @return
         * @throws InstantiationException
         * @throws IllegalAccessException
       */
        public static <T> List<T> mapsToObjects(List<Map<String, Object>> maps, Class<T> clazz)
                throws InstantiationException, IllegalAccessException {
            List<T> list = Lists.newArrayList();
            if (maps != null && maps.size() > 0) {
                Map<String, Object> map = null;
                T bean = null;
                for (int i = 0, size = maps.size(); i < size; i++) {
                    map = maps.get(i);
                    bean = clazz.newInstance();
                    mapToBean(map, bean);
                    list.add(bean);
                }
            }
            return list;
        }


文章作者: Athink
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Athink !
评论
  目录