Java List转Map

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.*;


/**
 *
 * 实体对象工具类
 *
 *
 */
public class EntityUtil {


    /**
     *
     * 将list中的元素放到Map<K, V>以建立 key - value 索引<p>
     *
     * @param list  List<V> 元素列表
     * @param keyFieldName String 元素的属性名称, 该属性的值作为索引key
     * @param <K> key类型
     * @param <V> value类型
     * @return Map<K, V> key - value 索引
     *
     *
     */
  @SuppressWarnings("unchecked")
    public static <K, V> Map<K, V> makeEntityMap(List<V> list, String keyFieldName) {
        Map<K, V> map = new HashMap<>();
        if(list == null || list.size() == 0) {
            return map;
        }
        try {
            Method getter = getMethod(list.get(0).getClass(), keyFieldName, "get");
            for (V item : list) {
                map.put((K) getter.invoke(item), item);
            }
        } catch (Exception e) {
            logger.error("makeEntityMap error list is " + list, e);
            return map;
        }
        return map;
    }


    /**
     *
     * 将list中的元素放到Map<String, V>以建立 key - value 索引<p>
     *
     * @param list  List<V> 元素列表
     * @param splitVar  属性之间间隔
     * @param keyFieldNames String 元素的属性名称动态数组, 依次循环该属性的值作为索引key
     * @param <V> value类型
     * @return Map<String, V> key - value 索引
     *
     *
     */


    @SuppressWarnings("unchecked")
    public static <V> Map<String, V> makeEntityMapByKeys(List<V> list,String splitVar , String... keyFieldNames) {
        Map<String, V> map = new HashMap<>();
        if(list == null || list.size() == 0 || keyFieldNames == null || keyFieldNames.length==0 || StringUtil.isStringEmpty(splitVar)) {
            return map;
        }
        try {
            List<Method> getterList = new ArrayList<>();
            for(String key : keyFieldNames){
                getterList.add(getMethod(list.get(0).getClass(),key, "get"));
            }
            for (V item : list) {
                StringBuffer keys= new StringBuffer("");
                for (int i=0;i<getterList.size();i++){
                    keys.append(getterList.get(i).invoke(item));
                    if(i<getterList.size()-1){
                        keys.append(splitVar);
                    }
                }
                map.put(keys.toString(), item);
            }
        } catch (Exception e) {
            logger.error("makeEntityMap error list is " + list, e);
            return map;
        }
        return map;
    }
    /**
     *
     * 将list中的元素放到Map<K, List<V>> 以建立 key - List<value> 索引<p>
     *
     * @param list  List<V> 元素列表
     * @param keyFieldName String 元素的属性名称, 该属性的值作为索引key
     * @param <K> key类型
     * @param <V> value类型
     * @return Map<K, V> key - value 索引
     *
     *
     */
    public static <K, V> Map<K, List<V>> makeEntityListMap(List<V> list, String keyFieldName) {
        Map<K, List<V>> map = new LinkedHashMap<>();
        if(list == null || list.size() == 0) {
            return map;
        }
        try {
            Method getter = getMethod(list.get(0).getClass(), keyFieldName, "get");
            for (V item : list) {
        @SuppressWarnings("unchecked")
                K key = (K) getter.invoke(item);
                List<V> groupList = map.get(key);
                if (groupList == null) {
                    groupList = new ArrayList<>();
                    map.put(key, groupList);
                }
                groupList.add(item);
            }
        } catch (Exception e) {
            logger.error("makeEntityListMap error list is " + list, e);
            return map;
        }
        return map;
    }


    /**
     * 获取getter或setter
     */
  @SuppressWarnings("unchecked")
  private static Method getMethod(@SuppressWarnings("rawtypes") Class clazz, String fieldName,
      String methodPrefix) throws NoSuchMethodException {
        String first = fieldName.substring(0, 1);
        String getterName = methodPrefix + fieldName.replaceFirst(first, first.toUpperCase());
        return clazz.getMethod(getterName);
    }


    /**
     * 比较两个对象的值是否不同
     * @param obj1 对象1
     * @param obj2 对象2
     * @param <T> object
     * @return 若俩对象的值不相同则为true,反之为false
     */
    public static <T> Boolean findDifference(T obj1,T obj2){
        if(obj1 == null && obj2 == null){
            return false;
        }
        if(obj1 == null || obj2 == null){
            return true;
        }
        if(obj1 instanceof BigDecimal){
            return ((BigDecimal) obj1).compareTo((BigDecimal)obj2) != 0;
        }else {
            return !obj1.equals(obj2);
        }
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值