zoukankan      html  css  js  c++  java
  • 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);
            });
  • 相关阅读:
    AS将一个项目导入到另一个项目中
    Android Studio出现:Cause: unable to find valid certification path to requested target
    小米手机Toast带app名称
    PopupWindow 点击外部区域无法关闭的问题
    EditText inputType类型整理
    Fragment通过接口回调向父Activity传值
    Android selector一些坑
    Installation failed with message Failed to commit install session 634765663 with command cmd package
    旷视上海研究院机器人方向招聘
    语义SLAM的数据关联和语义定位(四)多目标测量概率模型
  • 原文地址:https://www.cnblogs.com/hyfl/p/12883073.html
Copyright © 2011-2022 走看看