我有一个Employee对象,它具有rowId,skillId和Student如下
[0, 20,"John"],
[1, 30,"Amy"],
[2, 20,"Tom"]
我需要这样的答复:
Map<skillId, List<student>>
20 , ["John","Tom"]
30 , [Amy]
有人可以帮忙吗?
答案 0 :(得分:2)
尝试使用var dateGroups = periodQuery.Select(x => x.Date).Distinct().OrderBy(x => x.Date); //working
var datesEx = dateGroups.Select(g => periodQuery.Where(x => g.Date == x.Date).FirstOrDefault()); //not working
和groupingBy
mapping
答案 1 :(得分:0)
请使用以下内容,
Map<Integer, List<Employee>> collect = employeeList.stream()
.collect(Collectors.groupingBy(Employee::getSkillId, Collectors.toList()));
答案 2 :(得分:0)
Map<Integer, List<String>> skillAndList = list.stream().collect(Collectors.groupingBy(Employee::getSkillId, Collectors.mapping(Employee::getStudent, Collectors.toList())));
System.out.println(skillAndList);
输出与您预期的相同-
{30=[Amy], 20=[John,Tom]}