新旧数据比较 直接可用

我自定义的函数式编程自定义接口 可替换为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自定义

相关推荐
python_chai1 小时前
Python核心数据结构详解:元组、集合与字典
java·数据结构·python
老华带你飞2 小时前
医药垃圾分类管理系统|基于SSM+vue医药垃圾分类管理系统的系统设计与实现(源码+数据库+文档)
java·数据库·vue·毕业设计·论文·ssm·医药垃圾分类管理系统
程序员小续2 小时前
React 多个 HOC 嵌套太深,会带来哪些隐患?
java·前端·javascript·vue.js·python·react.js·webpack
rider1893 小时前
【9】搭建k8s集群系列(二进制部署)之安装work-node节点组件(kube-proxy)和网络组件calico
java·容器·kubernetes
ゞ 正在缓冲99%…3 小时前
leetcode274.H指数
java·算法·leetcode
旷野本野5 小时前
【Java】Maven
java·开发语言·maven
坊钰5 小时前
【MySQL 数据库】数据类型
java·开发语言·前端·数据库·学习·mysql·html
药尘师5 小时前
idea运行tomcat项目,很慢的问题
java·ide·intellij-idea
Kale又菜又爱玩5 小时前
Sentinel全面解析与实战教程
java·spring·微服务·sentinel·springboot·springcloud
hxung5 小时前
springboot项目中常用的工具类和api
数据库·spring boot·后端