jdk1.8的stream对两个 List 遍历匹配数据的处理

Posted ispotu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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());
        /**
         * 求差集:失败结果参考
         * [AwardInfouserId='null', stockNo='1', thirdStockNo='null',
         * AwardInfouserId='null', stockNo='2', thirdStockNo='null',
         * AwardInfouserId='null', stockNo='3', thirdStockNo='null',
         * AwardInfouserId='null', stockNo='4', thirdStockNo='null',
         * AwardInfouserId='null', stockNo='5', thirdStockNo='null',
         * AwardInfouserId='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的结果没去掉!
         * [AwardInfouserId='null', stockNo='1', thirdStockNo='third1',
         * AwardInfouserId='null', stockNo='2', thirdStockNo='third2',
         * AwardInfouserId='null', stockNo='3', thirdStockNo='third3',
         * AwardInfouserId='null', stockNo='4', thirdStockNo='null',
         * AwardInfouserId='null', stockNo='5', thirdStockNo='null',
         * AwardInfouserId='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的结果已去掉
         * [AwardInfouserId='null', stockNo='1', thirdStockNo='third1',
         * AwardInfouserId='null', stockNo='2', thirdStockNo='third2',
         * AwardInfouserId='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 + '\\'' +
                    '';
        
    

 

以上是关于jdk1.8的stream对两个 List 遍历匹配数据的处理的主要内容,如果未能解决你的问题,请参考以下文章

JDK1.8新特性——Stream

jdk1.8新特性Stream流式处理

JDK1.8新特性:Collectors收集器类

jdk1.8

Stream流

JDK1.8之Stream