List<Employee> byEmpAge = employeeMapper.getByEmpAge(88);,我想输出这个List内的对象的详细信息,怎么写
时间: 2024-03-02 07:49:34 浏览: 28
你可以使用 for 循环遍历这个 List,然后对每个 Employee 对象调用其 toString() 方法来输出其详细信息。例如:
```
List<Employee> byEmpAge = employeeMapper.getByEmpAge(88);
for (Employee employee : byEmpAge) {
System.out.println(employee.toString());
}
```
其中,toString() 方法需要 Employee 类自己实现,可以按照自己的需求输出对象的详细信息。如果 Employee 类没有实现 toString() 方法,则默认会输出其对象的地址信息,不太友好。
相关问题
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。
List<String> list = Arrays.asList(arr);Arrays报错 List<String> list = Arrays.asList(arr); List<String> list = Arrays.asList(arr);
Arrays.asList()方法返回的是一个固定大小的List,不能进行添加或删除操作,只能进行修改操作。如果你想要一个可变的List,可以使用java.util.ArrayList来代替Arrays.asList()方法返回的List。如果你使用Arrays.asList()方法将int[]转成ArrayList,会出现类型不匹配的错误,因为Arrays.asList()方法只能将数组转换为List<Object>类型,而int[]是一个基本数据类型,不是一个对象。如果你想要将int[]转换为List<Integer>类型的ArrayList,可以使用Java 8中的IntStream来实现,例如:
```java
int[] arr = {1, 2, 3};
List<Integer> list = IntStream.of(arr).boxed().collect(Collectors.toList());
```
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)