<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
/** * 通過遍歷兩個List中按id屬性相等的歸結到resultList中 * @param oneList * @param twoList */ 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()); /** * 求差集:失敗結果參考 * [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<AwardInfo> list2 = prizeRecords.stream() .filter(map -> stockDTOList.stream().anyMatch(map1 -> map.getStockNo().equals(map1.getStockNo()))) .forEach(map -> { map.setThirdStockNo(map1.getThirdStockNo()); });*/ 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()); /** * 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<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()); /** * 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 + ''' + '}'; } } }
以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45