对比两个list<object> 得到相同数据 差异数据

本文提供了一组Java工具类,用于对比两个List中的对象,获取相同数据和差异数据。通过反射机制获取对象的指定字段值进行比较,支持不同类型的List对象比较。
摘要由CSDN通过智能技术生成
package com.lizi.admin.utils.contrast;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class EntityUtil {

private static String GET_METHOD_PREFIX = "get";

public static <T> Object getFieldValue(T t, String fieldName) throws IllegalArgumentException, NoSuchMethodException, SecurityException, IllegalAccessException, InvocationTargetException{
String methodName = getGetMethodRealName(fieldName);
if(methodName == null){
throw new  IllegalArgumentException("外部订单号不能为空");
}
Class<?> clazz = t.getClass();
Method getMethod = clazz.getMethod(methodName);
return getMethod.invoke(t);
}

private static String getGetMethodRealName(String fieldName){
if(fieldName == null || fieldName.length() ==0){
return null;
}
String getMethodRealName = GET_METHOD_PREFIX + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
return getMethodRealName;
}
}




package com.lizi.admin.utils.contrast;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ListUtils {

private static <T> boolean isBaseEqual(List<T> first, List<T> second){
      if(first == null || second ==  null || first.size() == 0 || second.size() == 0){
          return false;
      }
      return true;
  }
  
  /**
   * 获取在第一个集合中但是不在第二个集合中的数据
   * @param first 第一个集合
   * @param second 第二个集合 
   * @param compareFieldName 需要比较的属性名称
   * @return 
   * @throws IllegalArgumentException
   * @throws NoSuchMethodException
   * @throws SecurityException
   * @throws IllegalAccessException
   * @throws InvocationTargetException
   */
  public static <T> List<T> getInFirstNotInSecondList(List<
  T> first, List<T> second, String compareFieldName) throws IllegalArgumentException, NoSuchMethodException, SecurityException, IllegalAccessException, InvocationTargetException{
      List<T> result = new ArrayList<>();
      if(isBaseEqual(first, second)){
      Map<String, String> listMap = new HashMap<>();
          String temp;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值