jdk8 stream流式处理替换for循环

1、for循环

        int size = chargeInfos.size();
        for (int i = 0; i < size; i++) {
            ChargeInfo chargeInfo = chargeInfos.get(i);
            String deviceNumber = chargeInfo.getDeviceNumber();
            String connectorId = chargeInfo.getConnectorId();

            // 2.1 按桩号分类端口
            if (!keyMap.containsKey(deviceNumber)) {
                List conList = new ArrayList<>();
                conList.add(connectorId);
                keyMap.put(deviceNumber, conList);
            } else {
                List conList = keyMap.get(deviceNumber);
                if (!conList.contains(connectorId)) {
                    conList.add(connectorId);
                }
                keyMap.put(deviceNumber, conList);
            }

            // 2.2 按桩号分类数据
            String info = stringRedisTemplate.opsForValue().get(deviceNumber);
            List lists = null;
            if (info != null) {
                lists = JsonUtil.json2List(info, ChargeInfo.class);
            }
            if (lists == null) {
                lists = new ArrayList<>();
            }
            lists.add(chargeInfo);
            stringRedisTemplate.opsForValue().set(deviceNumber, JsonUtil.list2Json(lists)); // 每个桩号对应10分钟内上报的数据集合
            System.out.println("i=" + i);
        }

 

2、stream流处理,静态方式Stream.iterate()创建无限流

        Map> keyMap = new HashMap<>();
        Stream.iterate(0, i -> i + 1).limit(chargeInfos.size()).forEach(i -> { // stream流式处理
            ChargeInfo chargeInfo = chargeInfos.get(i);
            String deviceNumber = chargeInfo.getDeviceNumber();
            String connectorId = chargeInfo.getConnectorId();

            // 2.1 按桩号分类端口
            if (!keyMap.containsKey(deviceNumber)) {
                List conList = new ArrayList<>();
                conList.add(connectorId);
                keyMap.put(deviceNumber, conList);
            } else {
                List conList = keyMap.get(deviceNumber);
                if (!conList.contains(connectorId)) {
                    conList.add(connectorId);
                }
                keyMap.put(deviceNumber, conList);
            }

            // 2.2 按桩号分类数据
            String info = stringRedisTemplate.opsForValue().get(deviceNumber);
            List list = null;
            if (info != null) {
                list = JsonUtil.json2List(info, ChargeInfo.class);
            }
            if (list == null) {
                list = new ArrayList<>();
            }
            list.add(chargeInfo);
            stringRedisTemplate.opsForValue().set(deviceNumber, JsonUtil.list2Json(list)); // 每个桩号对应10分钟内上报的数据集合
            list.clear();
            System.out.println("i=" + i);
        });
        chargeInfos.clear();

 

3、并行流式处理

        chargeInfos.parallelStream().forEach(chargeInfo -> {
            String deviceNumber = chargeInfo.getDeviceNumber();
            String connectorId = chargeInfo.getConnectorId();

            // 2.1 按桩号分类端口
            if (!keyMap.containsKey(deviceNumber)) {
                List conList = new ArrayList<>();
                conList.add(connectorId);
                keyMap.put(deviceNumber, conList);
            } else {
                List conList = keyMap.get(deviceNumber);
                if (!conList.contains(connectorId)) {
                    conList.add(connectorId);
                }
                keyMap.put(deviceNumber, conList);
            }

            // 2.2 按桩号分类数据
            List list = map.get(deviceNumber);
            if (list == null) {
                list = new ArrayList<>(10000);
            }
            list.add(chargeInfo);
            map.put(deviceNumber, list); // 每个桩号对应10分钟内上报的数据集合
        });
        log.info("数据量={}", chargeInfos.size());
        chargeInfos.clear();

实际批任务处理测试中,流式处理比for高效很多,数据量越大差距越明显,实际测试中5000条数据维持在100ms左右

你可能感兴趣的:(1.1,Java基础重新学习)