[工具类] Map的util包, 常用 实体类转化为map等操作

时间:2025-02-16 10:55:46

目录

 map工具类 常用

例子: json串转换map

例子: 将map的key放入list

例子: 将实体类转化为map


 map工具类 常用

例子: json串转换map

例子: 将map的key放入list

例子: 将实体类转化为map

等等 <==> 操作 整合

package ;

import ;
import org.;
import org.;

import ;
import ;
import ;
import ;
import ;
import ;
import .*;

/**
 * @version 1.0.0
 */
public class MapUtil {
    private static final Logger logger = ();

    /**
     * 复制 MAP, 浅拷贝
     * @param source 数据源
     * @param <T> 实体BEAN
     * @return 新map
     */
    public static <T> Map<String, T> cloneMap(Map<String, T> source){
        Map<String, T> target = new HashMap<>();
        for (<String, T> entry : ()) {
            String key = ();
            T value = ();
            (key, value);
        }
        return target;
    }

    /**
     * 对List进行分组存储
     * @param list 数据源
     * @param keyProperty KEY属性名
     * @return groupMap
     */
    public static <V> Map<String, List<Map<String, V>>> groupListToMap(List<Map<String, V>> list, String keyProperty){
        Map<String, List<Map<String, V>>> groupMap = new LinkedHashMap<>();
        for (Map<String, V> info : list){
            V key = (keyProperty);
            if(key == null) { continue; }
            List<Map<String, V>> value = (());
            if (value == null){
                value = new ArrayList<>();
                ((), value);
            }
            (info);
        }
        return groupMap;
    }

    /**
     * 对List进行分组存储, 用于JSON字符串转换为MAP使用
     * @param list 数据源
     * @param keyProperty KEY属性名
     * @return groupMap
     */
    public static Map<String, List<Map>> groupListToMap2(List<Map> list, String keyProperty){
        Map<String, List<Map>> groupMap = new LinkedHashMap<>();
        for (Map info : list){
            Object key = (keyProperty);
            if(key == null) { continue; }
            List<Map> value = (());
            if (value == null){
                value = new ArrayList<>();
                ((), value);
            }
            (info);
        }
        return groupMap;
    }

    /**
     * 对List进行分组存储
     * @param list 数据源
     * @param keyProperty KEY属性名
     * @param <T> 实体BEAN
     * @return groupMap
     */
    public static <T> Map<String, List<T>> getGroupMap(List<T> list, String keyProperty){
        Map<String, List<T>> groupMap = new LinkedHashMap<>();
        for (T info : list){
            Object key = (info, keyProperty);
            if(key == null) { continue; }
            List<T> value = (());
            if (value == null){
                value = new ArrayList<>();
                ((), value);
            }
            (info);
        }
        return groupMap;
    }

    /**
     * 对List进行分组存储, key = id + property
     * @param list 数据源
     * @param keyProperty KEY属性名
     * @param idProperty ID
     * @param <T> 实体BEAN
     * @return
     */
    public static <T> Map<String, List<T>> getGroupMap(List<T> list, String keyProperty, String idProperty){
        Map<String, List<T>> groupMap = new LinkedHashMap<>();
        for (T info : list){
            Object property = (info, keyProperty);
            Object id = (info, idProperty);
            if(property == null || id == null) { continue; }
            String key = () +"-"+ ();
            List<T> value = (key);
            if (value == null){
                value = new ArrayList<>();
                (key, value);
            }
            (info);
        }
        return groupMap;
    }

    /**
     * 将map的key转换为List
     * @param source 数据源
     * @param <K> key
     * @param <V> value
     * @return list
     */
    public static <K, V> List<K> mapKeyToList(Map<K, V> source){
        List<K> list = new ArrayList<K>();
        for (<K, V> entry : ()) {
            K key = ();
            (key);
        }
        return list;
    }

    /**
     * 将map的value转换为List
     * @param rowInfo 数据源
     * @param <V> 实体BEAN
     * @return list
     */
    public static <K, V> List<V> mapValueToList(Map<K, V> rowInfo){
        List<V> list = new ArrayList<V>();
        for(Iterator<K> keyIt = ().iterator(); (); ){
            K key = ();
            V value = (key);
            (value);
        }
        return list;
    }

    /**
     * 将map的value转换为List, 将指定属性的值存入list
     * @param rowInfo 数据源
     * @param columns 指定的key
     * @param <T> 实体BEAN
     * @return list
     */
    public static <T> List<T> mapToList(Map<String, T> rowInfo, String columns[]){
        List<T> list = new ArrayList<T>();
        for(String col : columns){
            T value = (col);
            (value);
        }
        return list;
    }

    /**
     * map转list,将MAP的每个键值对转换为一个新的MAP,存储在list中
     * @param source
     * @return List
     */
    public static <K, V> List<Map<String, Object>> mapToList(Map<K, V> source){
        List<Map<String, Object>> list = new ArrayList<>();
        for (<K, V> entry : ()) {
            Map<String, Object> info = new HashMap<>();
            ("key", ());
            ("value", ());
            (info);
        }
        return list;
    }

    /**
     * 实体类转map
     *
     * @param obj
     * @return
     */
    public static Map<String, Object> convertBeanToMap(Object obj) {
        if (obj == null) { return null; }
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            BeanInfo beanInfo = (());
            PropertyDescriptor[] propertyDescriptors = ();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = ();
                // 过滤class属性
                if (!("class")) {
                    // 得到property对应的getter方法
                    Method getter = ();
                    Object value = (obj);
                    if (null == value) {
                        (key, "");
                    } else {
                        (key, value);
                    }
                }
            }
        } catch (Exception e) {
            ("convertBean2Map Error {}", e);
        }
        return map;
    }

    public static <T> Map convertMap(T obj){
        ObjectMapper objectMapper = new ObjectMapper();
        Map map = (obj, );
        return map;
    }

    public static <T> T convertBean(Map map, Class<T> clazz){
        ObjectMapper objectMapper = new ObjectMapper();
        T info = (map, clazz);
        return info;
    }

    /**
     * map转实体类
     * @param clazz 目标类
     * @param map 源数据
     * @return T
     */
    public static <T> T convertMapToBean(Class<T> clazz, Map<String, Object> map) {
        T obj = null;
        try {
            BeanInfo beanInfo = (clazz);
            obj = ();
            // 给 JavaBean 对象的属性赋值
            PropertyDescriptor[] propertyDescriptors = ();
            for (int i = 0; i < ; i++) {
                PropertyDescriptor descriptor = propertyDescriptors[i];
                String propertyName = ();
                if ((propertyName)) {
                    Object value = (propertyName);
                    Object[] args = new Object[1];
                    args[0] = value;
                    ().invoke(obj, args);
                }
            }
        } catch (Exception e) {
            ("convertMapToBean 实例化JavaBean失败 {}" + ());
        }
        return obj;
    }

    /**
     * 实体对象转成Map
     *
     * @param obj 实体对象
     * @return
     */
    public static Map<String, Object> object2Map(Object obj) {
        Map<String, Object> map = new HashMap<>();
        if (obj == null) {
            return map;
        }
        Class clazz = ();
        Field[] fields = ();
        try {
            for (Field field : fields) {
                (true);
                ((), (obj));
            }
        } catch (Exception e) {
            ();
        }
        return map;
    }

    /**
     * Map转成实体对象
     *
     * @param map   map实体对象包含属性
     * @param clazz 实体对象类型
     * @return
     */
    public static Object map2Object(Map<String, Object> map, Class<?> clazz) {
        if (map == null) {
            return null;
        }
        Object obj = null;
        try {
            obj = ();
            Field[] fields = ().getDeclaredFields();
            for (Field field : fields) {
                int mod = ();
                if ((mod) || (mod)) {
                    continue;
                }
                (true);
                (obj, (()));
            }
        } catch (Exception e) {
            ();
        }
        return obj;
    }

    /**
     * map排序,默认升序
     * @param data 数据源
     * @param key 排序字段
     * @return
     */
    public static Map<String, Integer> sort(Map<String, Integer> data, String key){
        return sort(data, key, true);
    }

    /**
     * map排序
     * @param data 数据源
     * @param key 排序字段
     * @param asc true asc | false desc
     * @return
     */
    public static Map<String, Integer> sort(Map<String, Integer> data, String key, boolean asc){
        List<Map<String, Object>> list = mapToList(data);
        (list, new Comparator<Map<String, Object>>() {
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                Integer v1 = ((key).toString());
                Integer v2 = ((key).toString());
                if (null == v1) {
                    return -1;
                }
                if (null == v2) {
                    return 1;
                }
                if(asc) { return (v2); }
                return (v1);
            }
        });

        Map<String, Integer> result = new LinkedHashMap<>();
        for (Map<String, Object> map : list){
            (("key").toString(), (("value").toString()) );
        }
        return result;
    }

}