java 将两个List对象合并并去重
第一种整个对象进行去重处理
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.Stream;
List<OrderAppointmentSales> orderAppointmentSales = obcOrderAppointmentSalesService.getOrderAppointmentSalesList(syncTime);
List<OrderAppointmentSales> orderAppointmentSales2 = obcOrderAppointmentSalesService.getOrderAppointmentSalesListByTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS").format(sas.getUpdateTime()));
//将数据list对象合并去重 使用Stream来帮我们完成操作
List<OrderAppointmentSales> ords = Stream.of(orderAppointmentSales, orderAppointmentSales2)
.flatMap(Collection::stream)
.distinct()
.collect(Collectors.toList());
第二种针对指定字段进行去重
List<ShipmentHeader> ords = new ArrayList<>();
ords.addAll(listByWms1);
ords.addAll(listByWms2);
List<ShipmentHeader> studentList = new ArrayList<>(ords.stream()
.collect(Collectors.toMap(ShipmentHeader::getId, Function.identity(), (oldValue, newValue) -> newValue))
.values());
Collectors.toMap需要使用三个参数的版本,前两个参数一个是keyMapper函数一个是valueMapper函数的,第三个参数BinaryOperator函数接口。BinaryOperator函数接收两个参数,一个oldValue,一个newValue。用于当key重复时的数据处理
惜秦皇汉武,略输文采;唐宗宋祖,稍逊风骚。
一代天骄,成吉思汗,只识弯弓射大雕。
俱往矣,数风流人物,还看今朝
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库