protected String primaryKey;
private List<T> addPart = new ArrayList<>();
private List<T> delPart = new ArrayList<>();
private List<T> remainedList = new ArrayList<>();
public CollectionHelper(List<T> oldDataList, List<T> newDataList, String primaryKey)
{
this.oldDataList = oldDataList; //1,2,3,4,5
this.newDataList = newDataList; //"","",1,2,5,6,7,8 -- > add "", "", 6,7,8 ; edit 1,2,5 ; remove 3,4
this.primaryKey = primaryKey;
if(StrUtil.isBlank(primaryKey)){return;}
/*新增*/
for(T newData: this.newDataList) {
boolean isNotFound = true;
Object newKeyValue = BeanUtil.getFieldValue(newData, primaryKey);
if(!ObjectUtils.isEmpty(newKeyValue)) {
for (T oldData: this.oldDataList) {
Object oldKeyValue = BeanUtil.getFieldValue(oldData, primaryKey);
if(oldKeyValue.equals(newKeyValue)){
isNotFound = false;
break;
}
}
}
if(isNotFound) {
addPart.add(newData);
}
}
/*修改*/
for(T newData: this.newDataList) {
boolean isFound = false;
Object newKeyValue = BeanUtil.getFieldValue(newData, primaryKey);
for (T oldData: this.oldDataList) {
Object oldKeyValue = BeanUtil.getFieldValue(oldData, primaryKey);
if(oldKeyValue.equals(newKeyValue)){
isFound = true;
break;
}
}
if(isFound) {
remainedList.add(newData);
}
}
/*删除*/
for(T oldData: this.oldDataList) {
boolean isNotFound = true;
Object oldKeyValue = BeanUtil.getFieldValue(oldData, primaryKey);
if(!ObjectUtils.isEmpty(oldKeyValue)) {
for (T newData: this.newDataList) {
Object newKeyValue = BeanUtil.getFieldValue(newData, primaryKey);
if(oldKeyValue.equals(newKeyValue)){
isNotFound = false;
break;
}
}
}
if(isNotFound) {
delPart.add(oldData);
}
}
}
public List<T> getAddPart() {
return addPart;
}
public List<T> getDelPart() {
return delPart;
}
public List<T> getEditPart() {
return remainedList;
}
`
CollectionHelper complexHelper = new CollectionHelper(oldPeopleList, newPeopleList, "uuid");
public class CollectionHelper
{
//老数据;
protected List oldDataList = new ArrayList();
//新数据;
protected List newDataList = new ArrayList();
}
`