Skip to content

CollectionHelper 我觉得这个更好些,对项目对象没有入侵性。 #1

Description

@jezzhu

`
CollectionHelper complexHelper = new CollectionHelper(oldPeopleList, newPeopleList, "uuid");

public class CollectionHelper
{
//老数据;
protected List oldDataList = new ArrayList();
//新数据;
protected List newDataList = new ArrayList();

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;
}

}
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions