服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java8的Lambda遍历两个List匹配数据方式

Java8的Lambda遍历两个List匹配数据方式

2022-08-15 09:56陌亡 Java教程

这篇文章主要介绍了Java8的Lambda遍历两个List匹配数据方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Lambda遍历两个List匹配数据

1. 定义一个静态方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
     *  通过遍历两个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方法测试

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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遍历匹配数据的处理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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 + '\'' +
                    '}';
        }
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/IT_hejinrong/article/details/89005790

延伸 · 阅读

精彩推荐
  • Java教程java中TreeMap排序的示例代码

    java中TreeMap排序的示例代码

    本篇文章主要介绍了java中TreeMap排序的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    AsuraDong3542021-02-07
  • Java教程Spring的Bean容器介绍

    Spring的Bean容器介绍

    今天小编就为大家分享一篇关于Spring的Bean容器介绍,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧...

    李灿辉5622021-06-30
  • Java教程在Windows系统下安装Thrift的方法与使用讲解

    在Windows系统下安装Thrift的方法与使用讲解

    今天小编就为大家分享一篇关于在Windows系统下安装Thrift的方法与使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一...

    灰灰是菇凉呀5772021-06-21
  • Java教程Spring数据访问模板化方法

    Spring数据访问模板化方法

    今天小编就为大家分享一篇关于Spring数据访问模板化,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧...

    李灿辉3722021-06-28
  • Java教程关于springboot集成swagger3时spring-plugin-core报错的问题

    关于springboot集成swagger3时spring-plugin-core报错的问题

    这篇文章主要介绍了关于springboot集成swagger3时spring-plugin-core报错的问题,本文给大家分享解决方法,对大家的学习或工作具有一定的参考借鉴价值,需要的朋...

    weixin_445096154692021-12-24
  • Java教程SpringMVC Controller 返回值的可选类型详解

    SpringMVC Controller 返回值的可选类型详解

    本篇文章主要介绍了SpringMVC Controller 返回值的可选类型详解 ,spring mvc 支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void,有兴趣的可以了解...

    xiepeixing5372020-09-25
  • Java教程快速了解Java中ThreadLocal类

    快速了解Java中ThreadLocal类

    这篇文章主要介绍了快速了解Java中ThreadLocal类,介绍了ThreadLocal 是什么,ThreadLocal的作用,ThreadLocal 原理等相关内容,具有一定参考价值,需要的朋友可以...

    mengwei11142021-02-18
  • Java教程java自定义枚举转换器示例

    java自定义枚举转换器示例

    这篇文章主要介绍了java自定义枚举转换器示例,需要的朋友可以参考下 ...

    Java教程网2492019-11-23