可用于数据对接时,筛选出新旧数据中有差异的对象;此方法需要引入HUTOOL工具
此方法只是用于思路展示;cn.hutool hutool-all5.7.16
大数据情况下,建议不要使用此反射方法,直接复制思路,替换list,快很多;不要问为什么,问就是反射;
附带用20000条数据下反射与不用的对比耗时
package com.ambition.hislink.utils;
import cn.hutool.core.builder.EqualsBuilder;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.StopWatch;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.util.*;
import java.util.concurrent.TimeUnit;
@Slf4j
public class ListCompareUtils {
public static Map> getListCompare(List newData, List oldData, String[] notCompare, String[] only) {
Map> map = new HashMap();
List updateList = new ArrayList<>();
for (int i = newData.size()-1; i >=0 ; i--) {
for (int j = oldData.size()-1; j >=0 ; j--) {
T t = newData.get(i);
T t1 = oldData.get(j);
if (judgeonly(only, t, t1)) {
if (!EqualsBuilder.reflectionEquals(t,t1,notCompare)) {
//当有修改时,加入updateList中,跳出旧oldData,继续遍历newData;
updateList.add(t);
}
newData.remove(i);
oldData.remove(j);
break;
}
}
}
log.info("insertList ->{},updateList ->{},deleteList->{}",newData.size(),updateList.size(),oldData.size());
map.put("insertList", newData);
map.put("updateList", updateList);
map.put("deleteList", oldData);
return map;
}
private static boolean judgeonly(String[] only, T one, T two) {
for (String onlyStr : only) {
Object onlyValue = ReflectUtil.getFieldValue(one, onlyStr);
Object onlyValue1 = ReflectUtil.getFieldValue(two, onlyStr);
if ((!ObjectUtil.isBasicType(onlyValue) && !(onlyValue instanceof String) )
|| (!ObjectUtil.isBasicType(onlyValue1) && !(onlyValue1 instanceof String) )
|| !ObjectUtil.equal(onlyValue, onlyValue1)) {
return false;
}
}
return true;
}
public static void main(String[] args) {
List value1 = new ArrayList<>();
List value2 = new ArrayList<>();
for (int i = 0; i < 20000; i++) {
DemoPojo pdaExecutionDrug = new DemoPojo();
pdaExecutionDrug.setBedno(i + "");
pdaExecutionDrug.setPatname("zhangshan"+i);
pdaExecutionDrug.setCureno("1234");
value1.add(pdaExecutionDrug);
}
for (int i = 2; i < 25000; i++) {
DemoPojo pdaExecutionDrug = new DemoPojo();
pdaExecutionDrug.setBedno(i + "");
pdaExecutionDrug.setPatname("zhangshan"+(i<15000?i : i+1));
pdaExecutionDrug.setCureno( "123");
value2.add(pdaExecutionDrug);
}
StopWatch stopWatch = DateUtil.createStopWatch();
stopWatch.start("用反射");
Map> listCompare = getListCompare(CollUtil.newArrayList(value1), CollUtil.newArrayList(value2), null, new String[]{"bedno"});
stopWatch.stop();
stopWatch.start("不用反射");
List update = new ArrayList<>();
for (int i = value1.size()-1; i >=0 ; i--) {
for (int j = value2.size()-1; j >=0 ; j--) {
DemoPojo t = value1.get(i);
DemoPojo t1 = value2.get(j);
if (t.getBedno().equals(t1.getBedno())) {
if (!EqualsBuilder.reflectionEquals(t,t1,"bedNo","cureno")){
update.add(t);
}
value1.remove(i);
value2.remove(j);
break;
}
}
}
log.info("insertList ->{},updateList ->{},deleteList->{}",value1.size(),update.size(),value2.size());
stopWatch.stop();
Console.log(stopWatch.prettyPrint(TimeUnit.SECONDS));
}
}
@Data
class DemoPojo {
private String bedno;
private String patname;
private String cureno;
}
2022-03-10 15:04:55.458 INFO --- [ main] c.a.hislink.utils.ListCompareUtils : insertList ->2,updateList ->19998,deleteList->5000 2022-03-10 15:04:56.453 INFO --- [ main] c.a.hislink.utils.ListCompareUtils : insertList ->2,updateList ->5000,deleteList->5000 StopWatch '': running time = 12 s --------------------------------------------- s % Task name --------------------------------------------- 000000011 92% 用反射 000000000 08% 不用反射



