Java8的Lambda遍历两个List匹配数据方式
目录
Lambda遍历两个List匹配数据1.定义一个静态方法2.Main方法测试3.输出结果jdk1.8的stream对两个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 + "\"" + "}"; } } }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
X 关闭
X 关闭
- 15G资费不大降!三大运营商谁提供的5G网速最快?中国信通院给出答案
- 2联想拯救者Y70发布最新预告:售价2970元起 迄今最便宜的骁龙8+旗舰
- 3亚马逊开始大规模推广掌纹支付技术 顾客可使用“挥手付”结账
- 4现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 5如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 6AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 7转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 8充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 9好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 10名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?