java - 如何从 Java Spring Boot 中的列表对象中获取数组列表?
问题描述
public class EmployeeDetails {
// this itself is an Array List defined in other model as List<EmployeeDetails> employeeDetails
String name;
String age;
List<EmployeeDetails> employee // How to get this ?
}
我在下面尝试过,但无法访问该数组列表中的所有值..
public List<EmployeeDetails> printEmployeeDetails(List<EmployeeDetails> data) {
return data.getEmployeeDetails().stream().findFirst().get().getEmployee()
// this is only giving me first list. How can i access all values present in this Array List ?
}
解决方案
为了从另一个流中获取列表的流,您可以使用 flatMap 运算符。
return data.getEmployeeDetails()
.stream()
.flatMap(employee -> employee.getEmployee().stream())
.collect(Collectors.toList());
我不确定这个答案是否足够,您没有指定它是否应该递归地获取所有员工以及重复项会发生什么。
推荐阅读
- php - 使用 JWT 连接到 DocuSign API 的后端计划脚本
- spring-boot - Hikari 连接没有超时
- ethereum - 无法通过 Remix 部署简单的 Solidity 合约
- python - Pyspark UDF - TypeError:“模块”对象不可调用
- c# - 如何使用 AutoIT 清除远程桌面连接中的剪贴板
- spring - 具有依赖注入的视图构造函数抛出 NPE
- angular - 如何在 Ionic 3 中以 HH:MM 格式显示时间
- android-studio - 未找到 Android SDK 缺少 SDK 工具目录
- php - 混合内容 - mshots
- bash - 如果在目录中,文件大小小于 2.1kb 将文件移动到另一个目录