原本需求:一个二级 list 更新功能。
常规实现:先删除原来的全部数据,再重新插入,这样就很耗时了,所以这里写一个工具类出来。
1. 如果有新增的数据:仅对这些数据新增
2. 如果有删除的数据:仅删除这些数据
3. 其他的话就是更新了。
工具类如下:
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;/*** @author SUN* @date 2023/10/28*/
public class DynamicUpdateListUtil {/*** 获取需要更新的数据* @param allList 原来的全部数据,将目前数据的数据查出来 传过来* @param handlerList 当前请求的数据列表,也就是前端接口传来的最新 list* @param column 唯一标识列,一般传 id* @return 返回需要更新的列表【addList:直接插入数据库;updateList:直接更新;deleteList:直接删除的】* @param <T> 操作的 list 里面的对象类型*/public static <T> DynamicUpdateList<T> get(List<T> allList, List<T> handlerList, Function<T, ?> column) {DynamicUpdateList<T> objectDynamicUpdateList = new DynamicUpdateList<>();Set<?> handlerSet = handlerList.stream().map(column).collect(Collectors.toSet());Set<?> allSet = allList.stream().map(column).collect(Collectors.toSet());// 本次要删除的List<T> deleteList = allList.stream().filter(all -> !handlerSet.contains(column.apply(all))).toList();// 本次要添加的List<T> addList = handlerList.stream().filter(handler -> !allSet.contains(column.apply(handler))).toList();// 本次要更新的List<T> updateList = handlerList.stream().filter(handler -> allSet.contains(column.apply(handler))).toList();objectDynamicUpdateList.setAddList(addList);objectDynamicUpdateList.setUpdateList(updateList);objectDynamicUpdateList.setDeleteList(deleteList);return objectDynamicUpdateList;}public static class DynamicUpdateList<T> {/*** 本次需要新增的数据*/private List<T> addList;/*** 本次需要更新的数据*/private List<T> updateList;/*** 本次需要删除的数据*/private List<T> deleteList;public List<T> getAddList() {return addList;}public void setAddList(List<T> addList) {this.addList = addList;}public List<T> getUpdateList() {return updateList;}public void setUpdateList(List<T> updateList) {this.updateList = updateList;}public List<T> getDeleteList() {return deleteList;}public void setDeleteList(List<T> deleteList) {this.deleteList = deleteList;}@Overridepublic String toString() {return "DynamicUpdateList: " + "\n addList=" + addList + " \n updateList=" + updateList + " \n deleteList="+ deleteList;}}}
用法
具体参数如何传,已经写在了工具类的注释中。
import java.util.List;public class Main {public static void main(String[] args) {User user1 = new User(1L, "user1");User user1_1 = new User(1L, "user2");User user2 = new User(2L, "user2");User user3 = new User(3L, "user3");List<User> allList = List.of(user1, user2, user3);List<User> update = List.of(user1_1, user2);DynamicUpdateListUtil.DynamicUpdateList<User> dynamicUpdateList = DynamicUpdateListUtil.get(allList, update,User::getId);List<User> addList = dynamicUpdateList.getAddList();List<User> updateList = dynamicUpdateList.getUpdateList();List<User> deleteList = dynamicUpdateList.getDeleteList();System.out.println(dynamicUpdateList);}}
User.Java 测试用的
/*** @author SUN* @date 2023/10/28*/
public class User {private Long id;private String name;public User() {}public User(Long id, String name) {this.id = id;this.name = name;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "User{" + "id=" + id + ", name='" + name + '\'' + '}';}
}