ListChangeUtil工具类,对比差异度ADD增量集、DELETE减量集

import java.util.*;

/**
 * @author chenyb
 * @version 1.0.0
 * @jdk 1.8
 * @Description List 变化处理
 * @Result.getAddList 增量集合
 * @Result.getDeleteList 减量集合
 * @date 2023/7/25
 */
public class ListChangeUtil {


    public static class Result{
        private List addList;
        private List deleteList;

        public List getAddList() {
            return addList;
        }

        public void setAddList(List addList) {
            this.addList = addList;
        }

        public List getDeleteList() {
            return deleteList;
        }

        public void setDeleteList(List deleteList) {
            this.deleteList = deleteList;
        }
    }

    public static  Result change(final List oldList, final List newList) {
        Result result = new Result();
        // 过滤无效对比
        if (null == oldList || 0 == oldList.size()) {
            if (null != newList && 0 < newList.size()) {
                result.setAddList(newList);
            }
            return result;
        } else if (null == newList || 0 == newList.size()) {
            result.setDeleteList(oldList);
            return result;
        }

        // 增量集合
        List addList = new ArrayList<>();
        newList.stream().filter(nf -> !oldList.contains(nf)).forEach(addList::add);
        result.setAddList(addList);

        // 减量集合
        List deleteList = new ArrayList<>();
        oldList.stream().filter(of -> !newList.contains(of)).forEach(deleteList::add);
        result.setDeleteList(deleteList);
        return result;
    }
}

你可能感兴趣的:(Utils,java,mr.chenyb)