【发布时间】:2021-12-22 11:38:04
【问题描述】:
我有 Employee 表,其中包含列 dept、dept_id、floor、bonus_appl、first_nm、last_nm、salary、experience。
等效的 Pojo 看起来像
class Employee{
String department;
String departmentId;
int floor;
Boolean isBonusApplicable;
String firstName;
String lastName;
Long salary;
int experience;
}
我可以使用 JDBC 模板从数据库中获取结果集并创建员工列表。
现在,我想使用 Stream 将这个类的所有公共变量(即 department、department ID 等)组合在一起,这样我的 Pojo 就不会重复公共变量
{
"department" : [
{
"department" : "HR",
"departmentId" : "EMP_SECTION_1",
"floor" : 2,
"isBonusApplicable" : false,
"employees": [
{
"firstName" : "sunil",
"lastName" : "kumar",
"salary" : 100000,
"experience" : 10
},
{
"firstName" : "Anitha",
"lastName" : "Shri",
"salary" : 55000,
"experience" : 7
}
]
},
{
"department" : "Network",
"departmentId" : "EMP_SECTION_7",
"floor" : 1,
"isBonusApplicable" : true,
"employees": [
{
"firstName" : "Harish",
"lastName" : "Shah",
"salary" : 12000,
"experience" : 1
},
{
"firstName" : "Vignesh",
"lastName" : "Murthy",
"salary" : 25000,
"experience" : 3
}
]
}
]
}
有人可以帮助我如何用一段代码将它们组合在一起。流式 API 新手
List<Employee> employeeList = dao.getEmployeeList();
System.out.println(employeeList.size()); //returns 276 employee details spanning across 7 different departments
【问题讨论】:
标签: java json collections java-stream pojo