![](/img/trans.png)
[英]convert List<E> to Map<String, List<String>> using java 8 streams
[英]How to convert List<Object> to Map<String , List<String>> using java 8 streams?
我有Employee對象,其中具有rowId,skillId和Student如下
[0, 20,"John"],
[1, 30,"Amy"],
[2, 20,"Tom"]
我需要這樣的回應:
Map<skillId, List<student>>
20 , ["John","Tom"]
30 , [Amy]
有人可以幫忙嗎?
嘗試使用groupingBy
和mapping
list.stream()
.collect(Collectors.groupingBy(Employee::getSkillId,
Collectors.mapping(Employee:getStudent, Collectors.toList())));
請使用以下內容,
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);
輸出與您預期的相同-
{30=[Amy], 20=[John,Tom]}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.