List<T> 与 List<T> 根据主键融合成一个

业务背景:
在工作经常出现俩个List 对象 按照业务需要将不同参数根据规则合并成一个List 所以写了一个简单的工具类。
技术:利用反射

package com.lxs.suanfa.test0914;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @description ComparisonUnitl
 * @date 2021/11/23 10:16
 * @author: LiuXS
 */
public class ComparisonUntil<T> {

    private static String key;

    /**
     * list 与 list 进行融合
     * @param searchKey  融合key
     * @param valueKey   融合value
     * @param gather1    主体
     * @param gather2    副体
     * @throws Exception  反射异常、自定义运行时异常
     */
    public void postList2ListByKey(String searchKey, String valueKey, List<T> gather1, List<T> gather2) throws Exception {

        if (ComparisonUntil.isEmpty(searchKey) || ComparisonUntil.isEmpty(valueKey)) throw new RuntimeException("input param:key is empty!");

        key = searchKey;
        Map<String, T> ga1 = List2Map(gather1);
        Map<String, T> ga2 = List2Map(gather2);

        key = valueKey;
        ga1.entrySet().stream().forEach(entry -> {
            try {
                Method setMethod = getSetMethod(entry.getValue().getClass());
                Method getMethod = getGetMethod(ga2.get(entry.getKey()).getClass());
                setMethod.invoke(entry.getValue(), new Object[]{getMethod.invoke(ga2.get(entry.getKey()), new Object[0])});
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("java reflect error!");
            }
        });

    }

    private  Method getGetMethod(Class<? extends Object> aClass) throws Exception {
        StringBuilder methodName = new StringBuilder("get");
        methodName.append(key.substring(0, 1).toUpperCase());
        methodName.append(key.substring(1));

        Method method = aClass.getMethod(methodName.toString());
        method.setAccessible(true);
        return method;
    }

    private  Method getSetMethod(Class<? extends Object > aClass) throws Exception {
        StringBuilder methodName = new StringBuilder("set");
        methodName.append(key.substring(0, 1).toUpperCase());
        methodName.append(key.substring(1));

        Field field = aClass.getDeclaredField(key);
        Class[] parameterTypes = new Class[1];
        parameterTypes[0] =field.getType();

        Method method = aClass.getMethod(methodName.toString(), parameterTypes);
        method.setAccessible(true);
        return method;
    }

    private Map<String, T> List2Map(List<T> gather) throws RuntimeException {

        return gather.stream().collect(Collectors.toMap(n->{

            try {
                return getGetMethod(n.getClass()).invoke(n, new Object[0])+"";
            } catch (Exception e) {
                e.printStackTrace();
                RuntimeException get_by_key_error = new RuntimeException("N to Map: get by key error");
                throw get_by_key_error;
            }
        }, Function.identity(),(n1, n2)->n1));
    }

    public static boolean isEmpty(String var) {
        var=var.trim();
        return var == null || var.length() <= 0;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值