public static void main(String[] args) {
List<TestBody> oldInfoList = new ArrayList<>();
TestBody customer1 = new TestBody();
customer1.setId("old1");
customer1.setCustomerId("new111111");
oldInfoList.add(customer1);
TestBody customer2 = new TestBody();
customer2.setId("old2");
customer2.setCustomerId("new222222");
oldInfoList.add(customer2);
TestBody customer3 = new TestBody();
customer3.setId("old3");
customer3.setCustomerId("old888888");
oldInfoList.add(customer3);
TestBody customer4 = new TestBody();
customer4.setId("old4");
customer4.setCustomerId("old999999");
oldInfoList.add(customer4);
List<TestBody> latestInfoList = new ArrayList<>();
TestBody customerInfoDO1 = new TestBody();
customerInfoDO1.setId("new1");
customerInfoDO1.setCustomerId("new111111");
latestInfoList.add(customerInfoDO1);
TestBody customerInfoDO2 = new TestBody();
customerInfoDO2.setId("new2");
customerInfoDO2.setCustomerId("new222222");
latestInfoList.add(customerInfoDO2);
TestBody customerInfoDO3 = new TestBody();
customerInfoDO3.setId("new3");
customerInfoDO3.setCustomerId("new333333");
latestInfoList.add(customerInfoDO3);
TestBody customerInfoDO4 = new TestBody();
customerInfoDO4.setId("new4");
customerInfoDO4.setCustomerId("new444444");
latestInfoList.add(customerInfoDO4);
System.out.println("老数据:"+ oldInfoList);
System.out.println("新数据:"+ latestInfoList);
// 4.1、减量:oldSyncSmsInfoList(customerType:在保->非在保(["type1"]->["type5"]))
List<TestBody> reduceList = oldInfoList.stream().filter(x -> !latestInfoList.stream().map(TestBody::getCustomerId).collect(Collectors.toList())
.contains(x.getCustomerId())).collect(Collectors.toList());
if (!ObjectUtils.isEmpty(reduceList)) {
log.info("减量用户" + reduceList);
}
// 4.2、增量:(新增用户)
List<TestBody> addList = latestInfoList.stream().filter(x -> !oldInfoList.stream().map(TestBody::getCustomerId)
.collect(Collectors.toList()).contains(x.getCustomerId())).collect(Collectors.toList());
if (!ObjectUtils.isEmpty(addList)) {
log.info("新增用户:" + addList);
}
// 4.3、同量:更新用户数据,抓取最新的更新
List<TestBody> sameList = latestInfoList.stream().filter(x -> oldInfoList.stream().map(TestBody::getCustomerId).collect(Collectors.toList())
.contains(x.getCustomerId())).collect(Collectors.toList());
if (!ObjectUtils.isEmpty(sameList)) {
log.info("同量用户信息:" + sameList);
}
}
实现效果如下: