Java8中怎么用Lambda遍历两个List匹配数据
发表于:2024-10-11 作者:千家信息网编辑
千家信息网最后更新 2024年10月11日,这篇"Java8中怎么用Lambda遍历两个List匹配数据"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下
千家信息网最后更新 2024年10月11日Java8中怎么用Lambda遍历两个List匹配数据
这篇"Java8中怎么用Lambda遍历两个List匹配数据"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇"Java8中怎么用Lambda遍历两个List匹配数据"文章吧。
Lambda遍历两个List匹配数据
1. 定义一个静态方法
/** * 通过遍历两个List中按id属性相等的归结到resultList中 * @param oneList * @param twoList */ public static List
2. Main方法测试
public static void main(String[] args) { List> oneList = new ArrayList<>(); Map
3. 输出结果
jdk1.8的stream对两个List遍历匹配数据的处理
import java.util.ArrayList;import java.util.List;import java.util.Objects;import java.util.stream.Collectors; public class testStream { public static void main(String[] args) { ListprizeRecords = new ArrayList (6); List stockDTOList = new ArrayList (); for (int i = 0; i < 6; i++) { AwardInfo AwardInfo = new AwardInfo(); AwardInfo.setStockNo((i+1)+""); prizeRecords.add(AwardInfo); } for (int i = 0; i < 3; i++) { StockInfo stockDTO = new StockInfo(); stockDTO.setStockNo((i+1)+""); stockDTO.setThirdStockNo("third"+(i+1)); stockDTOList.add(stockDTO); } StockInfo stockDTO1 = new StockInfo(); stockDTO1.setStockNo((44)+""); stockDTO1.setThirdStockNo("third"+44); stockDTOList.add(stockDTO1); StockInfo stockDTO2 = new StockInfo(); stockDTO2.setStockNo((55)+""); stockDTO2.setThirdStockNo("third"+55); stockDTOList.add(stockDTO2); //prizeRecords与stockDTOList求差集 List resultList1 = prizeRecords.stream() .map(map -> stockDTOList.stream() .filter(m -> !Objects.equals(m.getStockNo(), map.getStockNo())) .findFirst().map(m -> { return map; }).orElse(null)) .filter(Objects::nonNull).collect(Collectors.toList()); /** * 求差集:失败结果参考 * [AwardInfo{userId='null', stockNo='1', thirdStockNo='null'}, * AwardInfo{userId='null', stockNo='2', thirdStockNo='null'}, * AwardInfo{userId='null', stockNo='3', thirdStockNo='null'}, * AwardInfo{userId='null', stockNo='4', thirdStockNo='null'}, * AwardInfo{userId='null', stockNo='5', thirdStockNo='null'}, * AwardInfo{userId='null', stockNo='6', thirdStockNo='null'}] */ System.out.println(resultList1.toString()); /* List list2 = prizeRecords.stream() .filter(map -> stockDTOList.stream().anyMatch(map1 -> map.getStockNo().equals(map1.getStockNo()))) .forEach(map -> { map.setThirdStockNo(map1.getThirdStockNo()); });*/ List resultList2 = prizeRecords.stream().map(m->{ stockDTOList.stream().filter(m2->Objects.equals(m.getStockNo(), m2.getStockNo())) .forEach(s-> m.setThirdStockNo(s.getThirdStockNo())); return m; }).collect(Collectors.toList()); /** * stockNo=4,5,6的结果没去掉! * [AwardInfo{userId='null', stockNo='1', thirdStockNo='third1'}, * AwardInfo{userId='null', stockNo='2', thirdStockNo='third2'}, * AwardInfo{userId='null', stockNo='3', thirdStockNo='third3'}, * AwardInfo{userId='null', stockNo='4', thirdStockNo='null'}, * AwardInfo{userId='null', stockNo='5', thirdStockNo='null'}, * AwardInfo{userId='null', stockNo='6', thirdStockNo='null'}] */ System.out.println(resultList2.toString()); List list3 = prizeRecords.stream() .map(map -> stockDTOList.stream() .filter(m -> Objects.equals(m.getStockNo(), map.getStockNo())) .findFirst().map(m -> { map.setThirdStockNo(m.getThirdStockNo()); return map; }).orElse(null)) .filter(Objects::nonNull).collect(Collectors.toList()); /** * stockNo=4,5,6的结果已去掉 * [AwardInfo{userId='null', stockNo='1', thirdStockNo='third1'}, * AwardInfo{userId='null', stockNo='2', thirdStockNo='third2'}, * AwardInfo{userId='null', stockNo='3', thirdStockNo='third3'}] */ System.out.println(list3.toString()); } static class StockInfo{ private String stockNo; private String stockName; private String thirdStockNo; public String getStockNo() { return stockNo; } public void setStockNo(String stockNo) { this.stockNo = stockNo; } public String getStockName() { return stockName; } public void setStockName(String stockName) { this.stockName = stockName; } public String getThirdStockNo() { return thirdStockNo; } public void setThirdStockNo(String thirdStockNo) { this.thirdStockNo = thirdStockNo; } @Override public String toString() { return "StockInfo{" + "stockNo='" + stockNo + '\'' + ", stockName='" + stockName + '\'' + ", thirdStockNo='" + thirdStockNo + '\'' + '}'; } } static class AwardInfo{ private String userId; private String stockNo; private String thirdStockNo; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getStockNo() { return stockNo; } public void setStockNo(String stockNo) { this.stockNo = stockNo; } public String getThirdStockNo() { return thirdStockNo; } public void setThirdStockNo(String thirdStockNo) { this.thirdStockNo = thirdStockNo; } @Override public String toString() { return "AwardInfo{" + "userId='" + userId + '\'' + ", stockNo='" + stockNo + '\'' + ", thirdStockNo='" + thirdStockNo + '\'' + '}'; } }}
以上就是关于"Java8中怎么用Lambda遍历两个List匹配数据"这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。
两个
数据
内容
结果
差集
文章
方法
知识
篇文章
金荣
价值
大部分
就是
属性
更多
步骤
知识点
行业
资讯
资讯频道
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
x79服务器内存条
网络安全防护的基本原则
五大连池市天气预报软件开发
盛邦网络安全科技有限公司
绿色上网网络安全
菏泽管理系统软件开发企业
java多线程实现数据库访问
数据库报错40001
linux用什么数据库好
方舟手游开服务器有啥用
win工控软件开发平台选择
怎样维护数据库
什么是bi数据库
三明飞鸽网络技术
郑州维爱互联网科技有限公司
软件开发一般都是什么人
软件开发要经历的几个阶段
志梦网络技术有限公司
oss做数据库json
香港数据库有哪一些
WIN8软件开发
互联网 农业 科技推广建议
软件开发时间和评估
阿里现在用的什么数据库
怎么屏蔽服务器请求
任子行网络技术是什么
科技有限公司可以做软件开发吗
比亚迪要做服务器吗
旌德先进软件开发服务解决方案
计算机年网络安全ppt模板