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);
        });
原文地址:https://www.cnblogs.com/hyfl/p/12883073.html