• java8-list转Map


    在获取数据需要查询多个表的时候,得到多个list集合来存储值。但是在取list集合几面的值的时候,是不能把list都嵌套的。
    那么就可以尝试这种方法,将list转成map,然后将表数据之间共同的那个字段作为Map的key。循环中根据key来取值
    Map<key类型,值类型> logMap = List.stream().collect(Collectors.toMap(v -> key, Function.identity()));
    代码演示:
    List<PhysicalPresentPO> phyList = presentIntentoryService.getPhyList(firstDayOfMonth, lastDayOfMonth);
            Integer countDeliver = presentIntentoryService.phyDeteilsList(firstDayOfMonth, lastDayOfMonth);
            List<Long> pid = new ArrayList<>();
            List<String> ordIdList = new ArrayList<>();
            phyList.forEach(v -> {
                pid.add(v.getPresentId());
                ordIdList.add(v.getOrderNo());
            });
    
            List<PresentLogisticsInfoPO> logList = presentIntentoryService.getLogList(ordIdList);
            Map<String, PresentLogisticsInfoPO> logMap = logList.stream().collect(Collectors.toMap(v -> v.getOrdersNoDicFk(), Function.identity()));
            List<PresentInventoryPO> inventList = presentIntentoryService.getInventList(pid);
            Map<Long, PresentInventoryPO> inventoryMap = inventList.stream().collect(Collectors.toMap(v -> v.getPresentId(), Function.identity()));
            List<Integer> uids = logList.stream().filter(v -> v.getDeliverId() !=null).map(v -> v.getDeliverId()).distinct().collect(Collectors.toList());
            List<Long> uidList = new ArrayList<>();
                    uids.forEach(u ->{
                        uidList.add(u.longValue());
            });
            cn.tanzhou.cheetah.response.result.ListResultSet<UserHighSensitiveFieldsDTO> userDto = iUserProvider.batchQryUserByUids(uidList, invokeParams);
            Map<Long, UserHighSensitiveFieldsDTO> userMap = userDto.getData().stream().collect(Collectors.toMap(v -> v.getUid(), Function.identity()));
            phyList.forEach(v -> {
                BigDecimal bigDecimal= new BigDecimal(v.getSendNum());
                PresentInventoryDTO presentInventoryDTO = new PresentInventoryDTO();
                presentInventoryDTO.setPresentName(v.getPresentName());
                presentInventoryDTO.setSendNum(v.getSendNum());
                presentInventoryDTO.setPresentPrice(v.getPresentPrice());
                presentInventoryDTO.setOutMonny(bigDecimal.multiply(v.getPresentPrice()));
                presentInventoryDTO.setSenderId(v.getSenderId());
                presentInventoryDTO.setCreateTime(v.getCreateTime());
                if(v.getOrderNo()!=null){
                    PresentLogisticsInfoPO presentLogisticsInfoPO = logMap.get(v.getOrderNo());
                    if(presentLogisticsInfoPO!=null) {
                        presentInventoryDTO.setInvoice(presentLogisticsInfoPO.getInvoice());
                        presentInventoryDTO.setDeliveryStatus(presentLogisticsInfoPO.getDeliveryStatus());
                    }
                }
                if(v.getPresentId()!=null){
                    PresentInventoryPO inventoryPO = inventoryMap.get(v.getPresentId());
                    if(inventoryPO!=null) {
                        presentInventoryDTO.setOwnDeptId(inventoryPO.getOwnDeptId());
                        presentInventoryDTO.setOwnDeptIdLink(inventoryPO.getOwnDeptIdLink());
                        presentInventoryDTO.setOwnDeptName(inventoryPO.getOwnDeptName());
                        presentInventoryDTO.setOwnDeptNameLink(inventoryPO.getOwnDeptNameLink());
                    }
                }
                UserHighSensitiveFieldsDTO userHighSensitiveFieldsDTO = userMap.get(v.getDeliverId());
                if(userHighSensitiveFieldsDTO!=null){
                    presentInventoryDTO.setDeliverName(userHighSensitiveFieldsDTO.getNick());
                    presentInventoryDTO.setAccount(userHighSensitiveFieldsDTO.getAccount());
                }else {
                    presentInventoryDTO.setDeliverName("-");
                    presentInventoryDTO.setAccount("-");
                }
    
                presentInventoryPOS.add(presentInventoryDTO);
            });
  • 相关阅读:
    [转载]ORACLE删除重复记录方法
    [转载]JAVA开发者最常去的20个英文网站
    [转载]Java常见异常汇总
    [转载]ORACLE删除重复记录方法
    《〈XNova/OGame〉源码笔记》(1-2)
    三拳两脚安装LUA
    一起复习几何(3)
    《〈XNova/OGame〉源码笔记》(3-4)
    喜迎四十万访问量,自荐十六篇好博文
    三招两式搞定修改VC项目名
  • 原文地址:https://www.cnblogs.com/hyfl/p/12883073.html
Copyright © 2020-2023  润新知