Set<Integer> headIds = feedbackSystemHeads.stream().map(FeedbackSystemHead::getHeadId).collect(Collectors.toSet()); 把headis转为Long集合 应该怎么修改
时间: 2024-02-06 10:09:48 浏览: 65
如果要将 `Set<Integer>` 转为 `Set<Long>`,可以使用以下代码:
```
Set<Long> headIds = feedbackSystemHeads.stream()
.map(FeedbackSystemHead::getHeadId)
.map(Long::valueOf)
.collect(Collectors.toSet());
```
该代码将 `FeedbackSystemHead` 中的 `headId` 属性转为 `Long` 类型,并将其收集到一个新的 `Set<Long>` 集合中。
相关问题
Set<Integer> titles = newsLs.stream().map(GoodNewsAccount::getAccountCount).filter(StringUtils::isNotBlank).collect(Collectors.toSet()); Condition.expectTrue(!CollectionUtils.isEmpty(titles), BizErrorEnum.ERROR_500002); 将filter后面的表达式换成判断integer
可以将filter后面的表达式改为以下形式:
```
.filter(count -> count != null && count > 0)
```
这样可以保证只有当 `count` 不为 null 且大于 0 时才会被保留在集合中。完整代码如下:
```
Set<Integer> titles = newsLs.stream()
.map(GoodNewsAccount::getAccountCount)
.filter(count -> count != null && count > 0)
.collect(Collectors.toSet());
Condition.expectTrue(!CollectionUtils.isEmpty(titles), BizErrorEnum.ERROR_500002);
```
优化下面的代码 public Page<FinanceCodeDTO> selectFinanceCodePage(FinanceCodeListVO financeCodeListVO) { //根据类型关联查询数据信息 Page<FinanceCodeDTO> page = new Page<>(financeCodeListVO.getPageNo(),financeCodeListVO.getPageSize()); Page<FinanceCodeDTO> dtoPage = financeCodeMapper.financeCodeList(page, financeCodeListVO); List<FinanceCodeDTO> dtos = dtoPage.getRecords(); List<String> contractIds = dtos.stream().map(FinanceCodePO::getContractIds).collect(Collectors.toList()); List<Integer> idList = new ArrayList<>(); contractIds.forEach(ids -> { if (ToolUtil.isNotEmpty(ids) && !ToolUtil.equals("[]",ids)){ List<Integer> list = JSONObject.parseArray(ids, Integer.class); idList.addAll(list); } }); if (ToolUtil.isNotEmpty(idList)){ CustomerContractPO customerContractPO = new CustomerContractPO(); List<CustomerContractPO> customerContractPOS = customerContractPO.selectList(new QueryWrapper<>(customerContractPO).lambda().in(CustomerContractPO::getCustomerContractId, idList)); Map<Integer, String> map = customerContractPOS.stream().collect(Collectors.toMap(CustomerContractPO::getCustomerContractId, CustomerContractPO::getContractName)); dtos.forEach(dto ->{ if (ToolUtil.isNotEmpty(dto.getContractIds())&& !ToolUtil.equals("[]",dto.getContractIds())){ List<Integer> list = JSONObject.parseArray(dto.getContractIds(), Integer.class); List<String> names = new ArrayList<>(); list.forEach(id ->{ names.add(map.get(id)); }); dto.setContractName(String.join(",",names)); } }); dtoPage.setRecords(dtos); } return dtoPage ; }
&& ids.contains(",")) { String[] idsArr = ids.split(","); Arrays.stream(idsArr).forEach(id -> { idList.add(Integer.parseInt(id)); }); } }); List<ContractDTO> contractDTOList = contractMapper.selectContractByIdList(idList); dtos.forEach(dto -> { dto.setContractList(contractDTOList); }); return dtoPage; }答:根据给定的代码,优化的思路是把contractIds通过Arrays.stream()和Integer.parseInt()转换为idList,并通过contractMapper.selectContractByIdList()获取contractDTOList,最后把contractDTOList赋给dtos中每个dto对象。
阅读全文