java8-list转Map
Posted 灰羽枫林
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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); });
以上是关于java8-list转Map的主要内容,如果未能解决你的问题,请参考以下文章
Java 遍历Map;Stream 对列表分组的groupBy
Java 遍历Map;Stream 对列表分组的groupBy
Java 遍历Map;Stream 对列表分组的groupBy
剑指offer知识点List转int[],List转String,String转int,char[]转String,String 转char[],List转String[]
剑指offer知识点List转int[],List转String,String转int,char[]转String,String 转char[],List转String[]