使用 Java 8 Stream实现List重复数据判断

打印 上一主题 下一主题

主题 1950|帖子 1950|积分 5850

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3. public class DeduplicateStreamExample {
  4.     static class ArchiveItem {
  5.         // 字段定义与Getter/Setter省略(需根据实际补充)
  6.         private String mATNR;
  7.         private String lIFNR;
  8.         private String suppSpecModel;
  9.         private String productName;
  10.         private String componentName;
  11.         private String dataProp;
  12.         private String dataType;
  13.         // 示例构造方法(需补充其他字段)
  14.         public ArchiveItem(String mATNR, String lIFNR, String suppSpecModel,
  15.                           String productName, String componentName,
  16.                           String dataProp, String dataType) {
  17.             this.mATNR = mATNR;
  18.             this.lIFNR = lIFNR;
  19.             this.suppSpecModel = suppSpecModel;
  20.             this.productName = productName;
  21.             this.componentName = componentName;
  22.             this.dataProp = dataProp;
  23.             this.dataType = dataType;
  24.         }
  25.         // 必须提供Getter方法
  26.         public String getMATNR() { return mATNR; }
  27.         public String getLIFNR() { return lIFNR; }
  28.         public String getSuppSpecModel() { return suppSpecModel; }
  29.         public String getProductName() { return productName; }
  30.         public String getComponentName() { return componentName; }
  31.         public String getDataProp() { return dataProp; }
  32.         public String getDataType() { return dataType; }
  33.     }
  34.     public static void main(String[] args) {
  35.         // 模拟数据(需补充完整)
  36.         List<ArchiveItem> archiveList = Arrays.asList(
  37.             new ArchiveItem("2010006", "100000", "123黑胡椒", "电池", "电池222", "成品", "POPS"),
  38.             new ArchiveItem("41200042", "100767", "dff", "fggg", "3455ff", "成品", "POPS")
  39.         );
  40.         // 核心逻辑:使用Stream分组并判断重复
  41.         Map<List<String>, List<ArchiveItem>> grouped = archiveList.stream()
  42.             .collect(Collectors.groupingBy(item ->
  43.                 Arrays.asList(
  44.                     item.getMATNR(),
  45.                     item.getLIFNR(),
  46.                     item.getSuppSpecModel(),
  47.                     item.getProductName(),
  48.                     item.getComponentName(),
  49.                     item.getDataProp(),
  50.                     item.getDataType()
  51.                 )
  52.             ));
  53.         // 提取重复条目(所有分组中数量>1的条目)
  54.         List<ArchiveItem> duplicates = grouped.values().stream()
  55.             .filter(group -> group.size() > 1)
  56.             .flatMap(List::stream)
  57.             .collect(Collectors.toList());
  58.         // 去重后的列表(每组取第一条)
  59.         List<ArchiveItem> uniqueList = grouped.values().stream()
  60.             .map(group -> group.get(0))
  61.             .collect(Collectors.toList());
  62.         // 输出结果
  63.         System.out.println("去重后的列表长度: " + uniqueList.size());
  64.         System.out.println("发现的重复条目数量: " + duplicates.size());
  65.         System.out.println("重复的条目: " + duplicates);
  66.     }
  67. }
复制代码
去重后的列表长度: 1
发现的重复条目数量: 2
重复的条目: [ArchiveItem@1, ArchiveItem@2]

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

笑看天下无敌手

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表