栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

根据元素某个字段对比两个集合差集-交集-补集

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

根据元素某个字段对比两个集合差集-交集-补集

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
public class CompareListUtil {

    
    public static String DIFFERENCE = "difference_add";

    
    public static String INTERSECTION = "intersection_update";

    
    public static String COMPLEMENT = "complement_delete";

    
    public static  Map> compareList(List oldList, List newList, String fieldName) throws IllegalAccessException {
        if (StringUtils.isBlank(fieldName)) {
            throw new RuntimeException("n对比字段不能为空");
        }
        // 空集合
        List emptyList = Lists.newArrayList();
        List differenceList = Lists.newArrayList();
        List intersectionList = Lists.newArrayList();
        List complementList = Lists.newArrayList();
        Map> resultMap = Maps.newHashMap();
        if (CollectionUtils.isEmpty(oldList)) {
            resultMap.put(DIFFERENCE, newList);
            resultMap.put(INTERSECTION, emptyList);
            resultMap.put(COMPLEMENT, emptyList);
            return resultMap;
        }
        if (CollectionUtils.isEmpty(newList)) {
            resultMap.put(DIFFERENCE, emptyList);
            resultMap.put(DIFFERENCE, emptyList);
            resultMap.put(COMPLEMENT, oldList);
            return resultMap;
        }

        Set existSet = Sets.newHashSet();
        for (T old : oldList) {
            Object fieldValue = getFieldValue(old, fieldName);
            existSet.add(fieldValue);
        }
        Set newSet = Sets.newHashSet();
        for (T newObj : newList) {
            Object fieldValue = getFieldValue(newObj, fieldName);
            newSet.add(fieldValue);
        }

        for (T newObj : newList) {
            Object fieldValueNew = getFieldValue(newObj, fieldName);
            if (existSet.contains(fieldValueNew)) {
                intersectionList.add(newObj);
            } else {
                differenceList.add(newObj);
            }
        }

        for (T old : oldList) {
            Object fieldValueNew = getFieldValue(old, fieldName);
            if (!newSet.contains(fieldValueNew)) {
                complementList.add(old);
            }
        }
        resultMap.put(DIFFERENCE, differenceList);
        resultMap.put(INTERSECTION, intersectionList);
        resultMap.put(COMPLEMENT, complementList);
        return resultMap;
    }

    
    private static  Object getFieldValue(T t, String fieldName) throws IllegalAccessException {
        Field targetField = Arrays.stream(t.getClass().getDeclaredFields())
                .filter(field -> field.getName().equals(fieldName)).collect(Collectors.toList()).get(0);
        targetField.setAccessible(true);
        return targetField.get(t);
    }

    public static void main(String[] args) throws IllegalAccessException {
        List usersOld = Lists.newArrayList();
        usersOld.add(new User(1L, "1"));
        usersOld.add(new User(2L, "1"));
        usersOld.add(new User(3L, "1"));
        usersOld.add(new User(4L, "1"));

        List usersNew = Lists.newArrayList();
        usersNew.add(new User(1L, "1"));
        usersNew.add(new User(2L, "1"));
        // usersNew.add(new User(3L, "1"));
        usersNew.add(new User(4L, "1"));
        usersNew.add(new User(5L, "1"));
        Map> maps = compareList(usersOld, usersNew, "userId");

        maps.forEach((k, v) -> {
            log.info("n {} : {}", k, JSONObject.toJSONString(v));
        });
    }

}

@Data
@AllArgsConstructor
class User {
    private Long userId;
    private String userName;
}
 
intersection_update : [{"userId":1,"userName":"1"},{"userId":2,"userName":"1"},{"userId":4,"userName":"1"}]
19:51:18.653 [main] INFO com.enterprise.common.util.tool.CompareListUtil - 
difference_add : [{"userId":5,"userName":"1"}]
19:51:18.653 [main] INFO com.enterprise.common.util.tool.CompareListUtil - 
complement_delete : [{"userId":3,"userName":"1"}]
转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号