List<DeductionJhCredit> deductionJhCreditReturns = (List<DeductionJhCredit>) jhCreditMap.values();这段代码有什么问题?
时间: 2024-05-20 21:16:30 浏览: 9
这段代码没有语法问题,但是它会引发一个警告(unchecked cast),因为将Map的values()方法的返回值强制转换为List<DeductionJhCredit>时,没有进行类型检查。如果Map中存储的对象类型与DeductionJhCredit不匹配,将会在运行时引发ClassCastException异常。建议使用泛型来避免这种类型转换问题。例如,可以使用Map<String, List<DeductionJhCredit>>来存储DeductionJhCredit对象,并且可以直接使用Map的get方法获取List<DeductionJhCredit>对象,而不需要强制类型转换。
相关问题
List<Users> us = new List<Users>();这段代码含义
这段代码定义了一个名为 `us` 的变量,类型为 `List<Users>`,表示创建了一个 `Users` 对象的列表。`List<Users>` 是 C# 泛型集合类,可以用来对 `Users` 类型的对象进行动态添加、删除、搜索等操作。这意味着,当你需要管理多个 `Users` 对象时,可以使用这个变量来存储它们,并在需要时进行操作。
List<ExpenseStatisticsDTO> expenseFlightByDept = travelExpensesRepository.getExpenseFlightByDept(expenseStatisticsDTO); List<ExpenseStatisticsDTO> expenseTrainByDept = travelExpensesRepository.getExpenseTrainByDept(expenseStatisticsDTO); List<ExpenseStatisticsDTO> expenseHotelByDept = travelExpensesRepository.getExpenseHotelByDept(expenseStatisticsDTO); List<ExpenseStatisticsDTO> expenseZSByDept = travelExpensesRepository.getExpenseZSByDept(expenseStatisticsDTO); List<ExpenseStatisticsDTO> expenseJTByDept = travelExpensesRepository.getExpenseJTByDept(expenseStatisticsDTO); List<ExpenseStatisticsDTO> expenseBTByDept = travelExpensesRepository.getExpenseBTByDept(expenseStatisticsDTO); List<ExpenseStatisticsDTO> expenseOtherByDept = travelExpensesRepository.getExpenseOtherByDept(expenseStatisticsDTO); Java把以上List合并成一个List,按ExpenseStatisticsDTO对象中structNo相同的情况下,则把totalPrice相加,最后返回List<ExpenseStatisticsDTO>
可以使用Java 8中的Stream API来实现List合并和按照structNo相同的情况下把totalPrice相加的操作。实现代码如下:
```java
List<ExpenseStatisticsDTO> mergedList = Stream.of(expenseFlightByDept, expenseTrainByDept, expenseHotelByDept, expenseZSByDept, expenseJTByDept, expenseBTByDept, expenseOtherByDept)
.flatMap(Collection::stream)
.collect(Collectors.groupingBy(ExpenseStatisticsDTO::getStructNo, Collectors.reducing(
new ExpenseStatisticsDTO("", "", 0.0),
(a, b) -> new ExpenseStatisticsDTO(b.getStructNo(), b.getStructName(), a.getTotalPrice() + b.getTotalPrice()),
(a, b) -> new ExpenseStatisticsDTO(b.getStructNo(), b.getStructName(), a.getTotalPrice() + b.getTotalPrice())
)))
.values()
.stream()
.sorted(Comparator.comparing(ExpenseStatisticsDTO::getStructNo))
.collect(Collectors.toList());
```
上述代码中,首先将多个List加入到一个Stream中,然后通过flatMap方法将多个Stream扁平化为一个Stream。接着使用groupingBy方法按structNo分组,使用reducing方法将同一组的ExpenseStatisticsDTO对象的totalPrice相加,并将结果转换为一个新的ExpenseStatisticsDTO对象。最后通过values方法取出所有的分组结果,通过sorted方法按照structNo排序,并通过collect方法将结果转换为List,即可得到合并后的List。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)