I have Employee object which has rowId, skillId and student as below
[0, 20,"John"],
[1, 30,"Amy"],
[2, 20,"Tom"]
and I need response like this:
Map<skillId, List<student>>
20 , ["John","Tom"]
30 , [Amy]
Can anyone help?
Try use groupingBy
and mapping
list.stream()
.collect(Collectors.groupingBy(Employee::getSkillId,
Collectors.mapping(Employee:getStudent, Collectors.toList())));
Please use the below,
Map<Integer, List<Employee>> collect = employeeList.stream()
.collect(Collectors.groupingBy(Employee::getSkillId, Collectors.toList()));
Map<Integer, List<String>> skillAndList = list.stream().collect(Collectors.groupingBy(Employee::getSkillId, Collectors.mapping(Employee::getStudent, Collectors.toList())));
System.out.println(skillAndList);
The output is the same as you expected -
{30=[Amy], 20=[John,Tom]}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.