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
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"}]