91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java8中怎么用Lambda遍歷兩個List匹配數據

發布時間:2022-03-02 16:50:53 來源:億速云 閱讀:1345 作者:iii 欄目:開發技術

這篇“Java8中怎么用Lambda遍歷兩個List匹配數據”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Java8中怎么用Lambda遍歷兩個List匹配數據”文章吧。

    Lambda遍歷兩個List匹配數據

    1. 定義一個靜態方法

    /**
         *  通過遍歷兩個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;
        }

    2. Main方法測試

    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);
        }

    3. 輸出結果

    Java8中怎么用Lambda遍歷兩個List匹配數據

    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) {
            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 + '\'' +
                        '}';
            }
        }
    }

    以上就是關于“Java8中怎么用Lambda遍歷兩個List匹配數據”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。

    向AI問一下細節

    免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

    AI

    凉城县| 苍梧县| 乌鲁木齐县| 安乡县| 大关县| 淄博市| 霞浦县| 中西区| 沂水县| 台中县| 永泰县| 阳朔县| 葵青区| 肥西县| 南江县| 柞水县| 宁蒗| 丽水市| 双峰县| 焉耆| 英德市| 息烽县| 宝坻区| 吐鲁番市| 成安县| 勃利县| 衡水市| 木里| 大悟县| 郑州市| 罗城| 团风县| 大安市| 东乌珠穆沁旗| 开封县| 衢州市| 弥渡县| 四子王旗| 永善县| 内丘县| 上林县|