当前位置: 首页 > news >正文

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

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]

相关文章:

  • C# 类型、存储和变量(类型是一种模板)
  • SQL Server 2022 安装常见问题及解决方法
  • AI编程新纪元:GitHub Copilot、CodeGeeX与VS2022的联合开发实践
  • CobaltStrike
  • 工作记录4
  • Spring Boot 中的自动配置原理
  • Flutter使用flutter_driver进行自动化测试
  • Python刷题笔记1
  • Golang|KVBitcask
  • springboot3 cloud gateway 配置websocket代理转发教程
  • [dp14_回文串] 分割回文串 II | 最长回文子序列 | 让字符串成为回文串的最少插入次数
  • 【JavaEE】Spring AOP的注解实现
  • Java大模型MCP服务端开发-数据库查询(智能问数)
  • 基于PLC的停车场车位控制系统的设计
  • Ubuntu 安装 NVIDIA显卡驱动、CUDA 以及 CuDNN工具
  • [ElasticSearch]Suggest查询建议(自动补全纠错)
  • 鸿蒙NEXT开发文件预览工具类(ArkTs)
  • IT运维常用的软件工具有哪些
  • iOS内存管理中的强引用问题
  • BGP(边界网关协议)
  • 给卖假性药的做网站一般要判多久/无锡网站建设方案优化
  • 百度网站收录链接提交/网络营销成功案例
  • 怎样增加网站流量/深圳推广优化公司
  • 怎么用电脑windows做网站/百度风云榜小说排行榜历届榜单
  • 青岛做网站建设价格低/seo是指什么
  • 爱搜索中级网站建设/大连网络推广