批量操作
批量操作 批量保存数据
页面的10条数据是一个List<SalaryLevelConf> newList;
数据库的数据是一个List<SalaryLevelConf> oldList;
1 2 3 4 <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> </dependency>
1 2 3 4 5 6 7 8 List<SalaryLevelConf> newList = new ArrayList<>(); List<SalaryLevelConf> oldList = new ArrayList<>(); List<SalaryLevelConf> intersectionUpdate = CollectionUtils.intersection(newList,oldList).stream().collect(Collectors.toList()); List<SalaryLevelConf> subtractAdd = CollectionUtils.subtract(newList,oldList).stream().collect(Collectors.toList()); List<SalaryLevelConf> subtractDelete = CollectionUtils.subtract(oldList,newList).stream().collect(Collectors.toList());
集合中的id字段求交集
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 List<SalaryLevelConf> newList = new ArrayList<>();// 页面数据 for (int i = 0; i < 3; i++) { SalaryLevelConf salaryLevelConf = new SalaryLevelConf(); salaryLevelConf.setId(String.valueOf(i)); newList.add(salaryLevelConf); } List<SalaryLevelConf> oldList = new ArrayList<>();// 数据库数据 for (int i = 0; i < 3; i++) { SalaryLevelConf salaryLevelConf = new SalaryLevelConf(); salaryLevelConf.setId(String.valueOf(i+2)); oldList.add(salaryLevelConf); } // 老ID集合 List<String> oldIdList = oldList.stream().map(SalaryLevelConf::getId).collect(Collectors.toList()); // 交集-更新 List<SalaryLevelConf> intersectionUpdate = newList.stream().filter( salaryLevelConf -> oldIdList.contains(salaryLevelConf.getId()) ).collect(Collectors.toList()); System.out.println(intersectionUpdate); // 新-旧 差集-新增 List<SalaryLevelConf> subtractAdd = newList.stream().filter( salaryLevelConf -> !oldIdList.contains(salaryLevelConf.getId()) ).collect(Collectors.toList()); System.out.println(subtractAdd); // 旧-新 差集-删除 List<String> newIdList = newList.stream().map(SalaryLevelConf::getId).collect(Collectors.toList()); List<SalaryLevelConf> subtractDelete = oldList.stream().filter( salaryLevelConf -> !newIdList.contains(salaryLevelConf.getId()) ).collect(Collectors.toList()); System.out.println(subtractDelete); @Data public class SalaryLevelConf { private String id; }