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]