Python 官方文档:入门教程 => 点击学习
目录Lambda遍历两个List匹配数据1.定义一个静态方法2.Main方法测试3.输出结果jdk1.8的stream对两个List遍历匹配数据的处理Lambda遍历两个List匹配
public static List<Map<Object, Object>> compareListHitData(List<Map<Object, Object>> oneList, List<Map<Object, Object>> twoList) {
List<Map<Object, Object>> resultList = oneList.stream().map(map -> twoList.stream()
.filter(m -> Objects.equals(m.get("id"), map.get("id")))
.findFirst().map(m -> {
map.putAll(m);
return map;
}).orElse(null))
.filter(Objects::nonNull).collect(Collectors.toList());
return resultList;
}
public static void main(String[] args) {
List<Map<Object, Object>> oneList = new ArrayList<>();
Map<Object, Object> oneMap = new HashMap<>();
oneMap.put("id", 111);
oneMap.put("userName", "何金荣");
Map<Object, Object> twoMap = new HashMap<>();
twoMap.put("id", 222);
twoMap.put("userName", "Hejinrong");
oneList.add(oneMap);
oneList.add(twoMap);
List<Map<Object, Object>> twoList = new ArrayList<>();
Map<Object, Object> threeMap = new HashMap<>();
threeMap.put("id", 111);
threeMap.put("userName", "何金荣");
Map<Object, Object> fourMap = new HashMap<>();
fourMap.put("id", 333);
fourMap.put("userName", "Hejinrong");
twoList.add(threeMap);
twoList.add(fourMap);
List<Map<Object, Object>> resultList = compareListHitData(oneList, twoList);
System.out.println(resultList);
}
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) {
List<AwardInfo> prizeRecords = new ArrayList<AwardInfo>(6);
List<StockInfo> stockDTOList = new ArrayList<StockInfo>();
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<AwardInfo> 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());
System.out.println(resultList1.toString());
List<AwardInfo> 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());
System.out.println(resultList2.toString());
List<AwardInfo> 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());
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 + '\'' +
'}';
}
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: Java8的Lambda遍历两个List匹配数据方式
本文链接: https://lsjlt.com/news/141056.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0