java8-list转Map
标签:取数据 ram null identity time() params distinct count bat
在获取数据需要查询多个表的时候,得到多个list集合来存储值。但是在取list集合几面的值的时候,是不能把list都嵌套的。
那么就可以尝试这种方法,将list转成map,然后将表数据之间共同的那个字段作为Map的key。循环中根据key来取值
Map logMap = List.stream().collect(Collectors.toMap(v -> key, Function.identity()));
代码演示:
List phyList = presentIntentoryService.getPhyList(firstDayOfMonth, lastDayOfMonth);
Integer countDeliver = presentIntentoryService.phyDeteilsList(firstDayOfMonth, lastDayOfMonth);
List pid = new ArrayList();
List ordIdList = new ArrayList();
phyList.forEach(v -> {
pid.add(v.getPresentId());
ordIdList.add(v.getOrderNo());
});
List logList = presentIntentoryService.getLogList(ordIdList);
Map logMap = logList.stream().collect(Collectors.toMap(v -> v.getOrdersNoDicFk(), Function.identity()));
List inventList = presentIntentoryService.getInventList(pid);
Map inventoryMap = inventList.stream().collect(Collectors.toMap(v -> v.getPresentId(), Function.identity()));
List uids = logList.stream().filter(v -> v.getDeliverId() !=null).map(v -> v.getDeliverId()).distinct().collect(Collectors.toList());
List uidList = new ArrayList();
uids.forEach(u ->{
uidList.add(u.longValue());
});
cn.tanzhou.cheetah.response.result.ListResultSet userDto = iUserProvider.batchQryUserByUids(uidList, invokeParams);
Map 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
标签:取数据 ram null identity time() params distinct count bat
原文地址:https://www.cnblogs.com/hyfl/p/12883073.html
评论