【发布时间】:2015-07-04 16:51:11
【问题描述】:
我有 Person 类:
import java.util.*;
public class Person {
private String name;
Map<String,Integer> Skills=new HashMap<>(); // skill name(String) and level(int)
public String getName(){
return this.name;
}
public Map<String,Integer> getSkills(){
return this.Skills;
}
}
还有App 类:
import java.util.*;
import java.util.Map.Entry;
import static java.util.stream.Collectors.*;
import static java.util.Comparator.*;
public class App {
private List<Person> people=new ArrayList<>(); // the people in the company
public Map<String,Set<String>> PeoplePerSkill(){
return this.people.stream().collect(groupingBy(p-> p.getSkills().keySet() //<-get
//^problem here
,mapping(Person::getName,toSet())));
}
}
在App 类中,PeoplePerSkill 方法需要返回每个技能的人名Set。这意味着一项技能可以为许多人所拥有。
我坚持使用groupingBy(p->p..........., ) 我只是无法获得技能名称的String,我尝试了很多方法但事情变得陌生:(。
顺便说一句,目前我的代码返回Map<Object, Set<String>>
【问题讨论】:
标签: java lambda java-8 java-stream