最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java8的Lambda遍歷兩個(gè)List匹配數(shù)據(jù)方式

 更新時(shí)間:2022年03月02日 14:43:52   作者:陌亡  
這篇文章主要介紹了Java8的Lambda遍歷兩個(gè)List匹配數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Lambda遍歷兩個(gè)List匹配數(shù)據(jù)

1. 定義一個(gè)靜態(tài)方法

/**
     *  通過遍歷兩個(gè)List中按id屬性相等的歸結(jié)到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. 輸出結(jié)果

jdk1.8的stream對兩個(gè)List遍歷匹配數(shù)據(jù)的處理

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());
        /**
         * 求差集:失敗結(jié)果參考
         * [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的結(jié)果沒去掉!
         * [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的結(jié)果已去掉
         * [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 + '\'' +
                    '}';
        }
    }
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Eclipse安裝配置方法圖文教程

    Eclipse安裝配置方法圖文教程

    這篇文章主要為大家詳細(xì)介紹了Eclipse安裝配置方法圖文教程,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Spring中統(tǒng)一異常處理示例詳解

    Spring中統(tǒng)一異常處理示例詳解

    這篇文章主要給大家介紹了關(guān)于Spring中統(tǒng)一異常處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • 最長重復(fù)子數(shù)組 findLength示例詳解

    最長重復(fù)子數(shù)組 findLength示例詳解

    今天給大家分享一道比較常問的算法面試題,最長重復(fù)子數(shù)組 findLength,文中給大家分享解題思路,結(jié)合示例代碼介紹的非常詳細(xì),需要的朋友參考下吧
    2023-08-08
  • Java多線程之中斷線程(Interrupt)的使用詳解

    Java多線程之中斷線程(Interrupt)的使用詳解

    interrupt字面上是中斷的意思,但在Java里Thread.interrupt()方法實(shí)際上通過某種方式通知線程,并不會(huì)直接中止該線程
    2013-05-05
  • 詳解Java多線程編程中的線程同步方法

    詳解Java多線程編程中的線程同步方法

    這篇文章主要介紹了Java多線程編程中的線程同步方法,使用synchronized關(guān)鍵字創(chuàng)建線程同步方法是實(shí)現(xiàn)線程同步的關(guān)鍵,需要的朋友可以參考下
    2016-05-05
  • 淺談Java內(nèi)省機(jī)制

    淺談Java內(nèi)省機(jī)制

    本文主要介紹了Java內(nèi)省機(jī)制,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • MyBatis通過BATCH批量提交的方法

    MyBatis通過BATCH批量提交的方法

    今天小編就為大家分享一篇關(guān)于MyBatis通過BATCH批量提交的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • rabbitmq延遲隊(duì)列的使用方式

    rabbitmq延遲隊(duì)列的使用方式

    這篇文章主要介紹了rabbitmq延遲隊(duì)列的使用方式,使用rabbitmq的延時(shí)隊(duì)列插件,實(shí)現(xiàn)同一個(gè)隊(duì)列中有多個(gè)不同超時(shí)時(shí)間的消息,并按時(shí)間超時(shí)順序出隊(duì),本文給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2024-04-04
  • SpringBoot接收與響應(yīng)xml報(bào)文請求的實(shí)現(xiàn)

    SpringBoot接收與響應(yīng)xml報(bào)文請求的實(shí)現(xiàn)

    我們在進(jìn)行接口對接時(shí),會(huì)出現(xiàn)報(bào)文形式的信息傳遞,這篇文章主要給大家介紹了關(guān)于SpringBoot接收與響應(yīng)xml報(bào)文請求的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • 解決mybatis無法給帶有下劃線屬性賦值問題

    解決mybatis無法給帶有下劃線屬性賦值問題

    這篇文章主要介紹了解決mybatis無法給帶有下劃線屬性賦值問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01

最新評論

辉南县| 德清县| 石泉县| 大同市| 南丰县| 虞城县| 连南| 塔河县| 安塞县| 大同县| 大埔区| 靖远县| 新源县| 麻江县| 都江堰市| 娱乐| 崇文区| 松原市| 南溪县| 乌鲁木齐市| 军事| 武强县| 泾源县| 贵港市| 竹山县| 桦甸市| 巫溪县| 辽中县| 南溪县| 武定县| 台前县| 黔东| 普洱| 镶黄旗| 崇阳县| 静海县| 华蓥市| 资源县| 武清区| 永清县| 河间市|