Issue
I wanna ask some thing Let's say I have list of employee, and each employee contains list of approvals.
And it looks like this
"employeeList": [
{
"id": 1,
"employee_name": "Johny Doe",
"approval": [
{
"title": "Manager 1",
"approved": true
},
{
"title": "Manager 2",
"approved": true
}
]
},
{
"id": 2,
"employee_name": "Jane Doe",
"approval": [
{
"title": "Manager 1",
"approved": true
},
{
"title": "Manager 2",
"approved": false
}
]
},
{
"id": 3,
"employee_name": "Falcon X",
"approval": [
{
"title": "Manager 1",
"approved": true
},
{
"title": "Manager 2",
"approved": true
}
]
}]
the thing is I wanna show list which only approved by Manager 1 and Manager 2, this is what it looks like
"employeeList": [
{
"id": 1,
"employee_name": "Johny Doe",
"approval": [
{
"title": "Manager 1",
"approved": true
},
{
"title": "Manager 2",
"approved": true
}
]
},
{
"id": 3,
"employee_name": "Falcon X",
"approval": [
{
"title": "Manager 1",
"approved": true
},
{
"title": "Manager 2",
"approved": true
}
]
}]
How to achieve this is in Java ? Can I have suggestion ? Any suggestion would be helped
Thanks in advance
Solution
Try this.
record Approval(String title, boolean approved) {}
record Employee(int id, String employee_name, List<Approval> approval) {}
List<Employee> employees = List.of(
new Employee(1, "Johny Doe",
List.of(new Approval("Manager 1", true), new Approval("Manager 2", true))),
new Employee(2, "Jane Doe",
List.of(new Approval("Manager 1", true), new Approval("Manager 2", false))),
new Employee(3, "Falcon X",
List.of(new Approval("Manager 1", true), new Approval("Manager 2", true))));
Approval m1 = new Approval("Manager 1", true),
m2 = new Approval("Manager 2", true);
List<Employee> result = employees.stream()
.filter(e -> e.approval.contains(m1) && e.approval.contains(m2))
.toList();
for (Employee e : result)
System.out.println(e);
output:
Employee[id=1, employee_name=Johny Doe, approval=[Approval[title=Manager 1, approved=true], Approval[title=Manager 2, approved=true]]]
Employee[id=3, employee_name=Falcon X, approval=[Approval[title=Manager 1, approved=true], Approval[title=Manager 2, approved=true]]]
If you use class
instead of record
, you need to override equals()
and hashCode()
appropriately.
Answered By - 英語は苦手
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.