马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import java.util.*;
- import java.util.stream.Collectors;
- public class DeduplicateStreamExample {
- static class ArchiveItem {
- // 字段定义与Getter/Setter省略(需根据实际补充)
- private String mATNR;
- private String lIFNR;
- private String suppSpecModel;
- private String productName;
- private String componentName;
- private String dataProp;
- private String dataType;
- // 示例构造方法(需补充其他字段)
- public ArchiveItem(String mATNR, String lIFNR, String suppSpecModel,
- String productName, String componentName,
- String dataProp, String dataType) {
- this.mATNR = mATNR;
- this.lIFNR = lIFNR;
- this.suppSpecModel = suppSpecModel;
- this.productName = productName;
- this.componentName = componentName;
- this.dataProp = dataProp;
- this.dataType = dataType;
- }
- // 必须提供Getter方法
- public String getMATNR() { return mATNR; }
- public String getLIFNR() { return lIFNR; }
- public String getSuppSpecModel() { return suppSpecModel; }
- public String getProductName() { return productName; }
- public String getComponentName() { return componentName; }
- public String getDataProp() { return dataProp; }
- public String getDataType() { return dataType; }
- }
- public static void main(String[] args) {
- // 模拟数据(需补充完整)
- List<ArchiveItem> archiveList = Arrays.asList(
- new ArchiveItem("2010006", "100000", "123黑胡椒", "电池", "电池222", "成品", "POPS"),
- new ArchiveItem("41200042", "100767", "dff", "fggg", "3455ff", "成品", "POPS")
- );
- // 核心逻辑:使用Stream分组并判断重复
- Map<List<String>, List<ArchiveItem>> grouped = archiveList.stream()
- .collect(Collectors.groupingBy(item ->
- Arrays.asList(
- item.getMATNR(),
- item.getLIFNR(),
- item.getSuppSpecModel(),
- item.getProductName(),
- item.getComponentName(),
- item.getDataProp(),
- item.getDataType()
- )
- ));
- // 提取重复条目(所有分组中数量>1的条目)
- List<ArchiveItem> duplicates = grouped.values().stream()
- .filter(group -> group.size() > 1)
- .flatMap(List::stream)
- .collect(Collectors.toList());
- // 去重后的列表(每组取第一条)
- List<ArchiveItem> uniqueList = grouped.values().stream()
- .map(group -> group.get(0))
- .collect(Collectors.toList());
- // 输出结果
- System.out.println("去重后的列表长度: " + uniqueList.size());
- System.out.println("发现的重复条目数量: " + duplicates.size());
- System.out.println("重复的条目: " + duplicates);
- }
- }
复制代码 去重后的列表长度: 1
发现的重复条目数量: 2
重复的条目: [ArchiveItem@1, ArchiveItem@2]
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |