新旧数据比较 直接可用

我自定义的函数式编程自定义接口 可替换为java.util.function.Function
java 复制代码
public interface RSupplier<T,V> {

    V apply(T t);
}
对比类
java 复制代码
package xxx.utils;

import xxx.RSupplier;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//T为实体类时需要重写hashCode和equals方法
public class NewOldComparer<T> {

	private List<T> insertObjs = new ArrayList<>();

	private List<T> updateObjs = new ArrayList<>();

	private List<T> deleteObjs = new ArrayList<>();

	public NewOldComparer(List<T> newObjs, List<T> oldObjs, RSupplier<T, Long> idSupplier) {
		if (null != newObjs && !newObjs.isEmpty()) {
			compareHisRecord(newObjs, oldObjs, idSupplier);
		} else {
			noHasNewIds(oldObjs);
		}
	}

	public List<T> insertObjs() {
		return insertObjs;
	}

	public List<T> updateObjs() {
		return updateObjs;
	}

	public List<T> deleteObjs() {
		return deleteObjs;
	}

	private void compareHisRecord(List<T> newObjects, List<T> oldObjects, RSupplier<T, Long> idSupplier) {
		if (oldObjects.isEmpty()) {
			this.insertObjs = newObjects;
		} else {
			Map<Long, T> oldObjMap = oldObjects.stream().collect(Collectors.toMap(idSupplier::apply, c -> c, (c1, c2) -> c1));
			Map<Long, T> newObjMap = newObjects.stream().collect(Collectors.toMap(idSupplier::apply, c -> c, (c1, c2) -> c1));
			for (T curObj : newObjects) {
				Long curId = idSupplier.apply(curObj);
				if (oldObjMap.get(curId) == null) {
					this.insertObjs.add(curObj);
				} else {
					T optObj = oldObjMap.get(idSupplier.apply(curObj));
					if (optObj != null && !optObj.equals(curObj)) {
						this.updateObjs.add(curObj);
					}
				}
			}
			for (T curOldObj : oldObjects) {
				if (newObjMap.get(idSupplier.apply(curOldObj)) == null) {
					this.deleteObjs.add(curOldObj);
				}
			}
		}
	}

	private void noHasNewIds(List<T> oldObjs) {
		if (null != oldObjs && !oldObjs.isEmpty()) {
			this.deleteObjs = oldObjs;
		}
	}
}
使用
java 复制代码
		//新数据
		List<Value> newValueList= new ArrayList<>();
		//旧数据
		List<Value> oldValueList= new ArrayList<>();
		//比较
		NewOldComparer<Value> comparer = new NewOldComparer<>(newValueList, oldValueList, Value::getRid);
        List<Value> insertList = comparer.insertObjs();
        List<Value> deleteList = comparer.deleteObjs();
        List<Value> updateList = comparer.updateObjs();
        //后续逻辑...

具体比较哪些字段,需要在对象重写的equals和hashCode自定义

相关推荐
小当家.10511 分钟前
深入理解JVM:架构、原理与调优实战
java·jvm·架构
太空眼睛11 分钟前
【MCP】使用SpringBoot基于Streamable-HTTP构建MCP-Server
spring boot·sse·curl·mcp·mcp-server·spring-ai·streamable
幽络源小助理17 分钟前
springboot校园车辆管理系统源码 – SpringBoot+Vue项目免费下载 | 幽络源
vue.js·spring boot·后端
刀法如飞19 分钟前
一款开箱即用的Spring Boot 4 DDD工程脚手架
java·后端·架构
一嘴一个橘子23 分钟前
spring-aop 的 基础使用 -3 - 切点表达式 的提取、复用
java
Re_zero25 分钟前
Java新手避坑:为什么我劝你放弃 scanner.nextInt()?
java
幽络源小助理41 分钟前
SpringBoot+Vue车票管理系统源码下载 – 幽络源免费项目实战代码
vue.js·spring boot·后端
猫吻鱼42 分钟前
【系列文章合集】【全部系列文章合集】
spring boot·dubbo·netty·langchain4j
Good_Starry1 小时前
Java——反射
java
又是忙碌的一天1 小时前
SpringBoot 创建及登录、拦截器
java·spring boot·后端