对比两个集合对象数据
进行数据的操作
package com.demo.module.resource.vo;
import lombok.Data;
import sun.text.resources.CollationData;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class VmNewOldCompare {
private List<VmInfo> addVmLists = new ArrayList<>();
private List<VmInfo> updateVmLists = new ArrayList<>();
private List<VmInfo> deleteVmLists = new ArrayList<>();
public List<VmInfo> addVmLists(){
return addVmLists;
}
public List<VmInfo> updateVmLists(){
return updateVmLists;
}
public List<VmInfo> deleteVmLists(){
return deleteVmLists;
}
public void VmNewOldCompare(List<VmInfo> newVmInfoList, List<VmInfo> dbVmInfoList) throws Exception {
if (newVmInfoList == null || newVmInfoList.size() == 0){
throw new Exception("获取的虚拟机新数据为空,无法进行更新操作");
}
handleNewOldVmCompare(newVmInfoList, dbVmInfoList);
}
private void handleNewOldVmCompare(List<VmInfo> newVmInfoList, List<VmInfo> dbVmInfoList) {
if (dbVmInfoList.size() == 0){
this.addVmLists = newVmInfoList;
}else{
List<String> newVmIds = newVmInfoList.stream().map(VmInfo::getVmId).collect(Collectors.toList());
List<String> dbVmIds = dbVmInfoList.stream().map(VmInfo::getVmId).collect(Collectors.toList());
for (VmInfo vmInfo : newVmInfoList) {
if (!dbVmIds.contains(vmInfo.getVmId())){
this.addVmLists.add(vmInfo);
}
}
for (VmInfo vmInfo : newVmInfoList) {
if (dbVmIds.contains(vmInfo.getVmId())){
this.updateVmLists.add(vmInfo);
}
}
for (VmInfo dbVmInfo : dbVmInfoList) {
if (!newVmIds.contains(dbVmInfo.getVmId())){
this.deleteVmLists.add(dbVmInfo);
}
}
}
}
}
批量添加
批量删除
批量更新