All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.starmcc.qmdata.util.ConvertUtil Maven / Gradle / Ivy

There is a newer version: 2.2.1-RELEASE
Show newest version
package com.starmcc.qmdata.util;


import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.sf.cglib.beans.BeanMap;

import java.util.List;
import java.util.Map;

/**
 * @author qm
 * @date 2018年11月24日 上午1:57:19
 * @Description List、Map工具类
 */
public final class ConvertUtil {

    private ConvertUtil() {
    }

    /**
     * @param bean
     * @return
     * @Description 将对象装换为map
     */
    public final static  Map beanToMap(T bean) {
        Map map = Maps.newHashMap();
        if (bean != null) {
            BeanMap beanMap = BeanMap.create(bean);
            for (Object key : beanMap.keySet()) {
                map.put(key + "", beanMap.get(key));
            }
        }
        return map;
    }

    /**
     * 将map装换为javabean对象
     *
     * @param map
     * @param bean
     * @return
     * @Description 将map装换为javabean对象
     */
    public final static  T mapToBean(Map map, T bean) {
        BeanMap beanMap = BeanMap.create(bean);
        beanMap.putAll(map);
        return bean;
    }

    /**
     * @param objList
     * @return
     * @Description 将List转换为List>
     */
    public final static  List> objectsToMaps(List objList) {
        List> list = Lists.newArrayList();
        if (objList != null && objList.size() > 0) {
            Map 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;
    }

    /**
     * @param maps
     * @param clazz
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @Description 将List>转换为List
     */
    public final static  List mapsToObjects(List> maps, Class clazz) {
        List list = Lists.newArrayList();
        if (maps != null && maps.size() > 0) {
            Map map = null;
            T bean = null;
            for (int i = 0, size = maps.size(); i < size; i++) {
                map = maps.get(i);
                try {
                    bean = clazz.newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                mapToBean(map, bean);
                list.add(bean);
            }
        }
        return list;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy