Java Stream遍历与一般的foreach循环处理压测分析

本文探讨了在1000并发情况下,Java Stream遍历与传统的foreach循环在处理数据时的性能表现。通过一个具体的代码实例,展示了如何查询门店信息,并进行条件过滤和转换操作。在优化方面,代码使用了BeanUtils复制属性,以及根据坐标计算距离。对于性能敏感的场景,文章可能涉及如何选择更优的数据处理方式。
摘要由CSDN通过智能技术生成

Java Stream遍历与一般的foreach循环处理压测分析

  • foreach 循环的压测 (1000并发)
    在这里插入图片描述

  • Java stram 遍历 (1000并发)

在这里插入图片描述

  • 代码实例(怎样优化代码?)
    @Override
    public List<StoreInfoDTO> getStoreInfoListSql(String storeId,Double longitude,Double latitude) {
        List<StoreInformation>  storeInfoList = null;
        try {
            QueryWrapper<StoreInformation> query = new QueryWrapper<>();
            query.eq(StoreInformation.STORE_STATE, StoreStatus.OPEN);//正常营业中
            if(StringUtils.isNotBlank(storeId)){
                query.eq(StoreInformation.STORE_CODE, storeId);
            }
            storeInfoList = storeInformationMapper.selectList(query);
        } catch (Exception e) {
            log.error("查询门店信息异常:storeId:{},经度longitude:{},纬度latitude:{}",storeId,longitude,latitude);
            e.printStackTrace();
        }

        List<StoreInfoDTO> storeList = Lists.newArrayList();
        if(null != storeInfoList && storeInfoList.size() > 0){
//            storeInfoList = storeInfoList.stream().filter(item -> Objects.equals(item.getStoreState(), StoreDailyStatus.OPEN)).collect(Collectors.toList());
//            storeList = storeInfoList.stream().map(item -> {
//                StoreInfoDTO storeLocationInfo = new StoreInfoDTO();
//                BeanUtils.copyProperties(item, storeLocationInfo);
//                //计算距离
//                if(null != longitude && null != latitude){
//                    double lon2 = item.getLongitude().doubleValue();
//                    double lat2 = item.getLatitude().doubleValue();
//                    Double distance = aMapWebApiCallService.getDistance(longitude,latitude,lon2,lat2);//计算两个坐标的距离
//                    storeLocationInfo.setDistance(distance.intValue());
//                }
//                return storeLocationInfo;
//            }).collect(Collectors.toList());

            for (StoreInformation storeInfo : storeInfoList) {
                if(Objects.equals(storeInfo.getStoreState(), StoreDailyStatus.CLOSE)){//过滤调已打烊的
                    continue;
                }
                StoreInfoDTO storeLocationInfo = new StoreInfoDTO();
                BeanUtils.copyProperties(storeInfo, storeLocationInfo);
                //计算距离
                if(null != longitude && null != latitude){
                    double lon2 = storeInfo.getLongitude().doubleValue();
                    double lat2 = storeInfo.getLatitude().doubleValue();
                    Double distance = aMapWebApiCallService.getDistance(longitude,latitude,lon2,lat2);//计算两个坐标的距离
                    storeLocationInfo.setDistance(distance.intValue());
                }
                storeList.add(storeLocationInfo);
            }
        }
        return storeList;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值