将列表流转换为单个容器
2022-09-03 18:35:12
考虑以下类:WorkExperience
public class WorkExperience {
private int year;
private List<Skills> skill;
public WorkExperience(int year, List<Skills> skill) {
this.year = year;
this.skill = skill;
}
//getter setter
}
public class Skills {
private String skills;
public Skills(String skills) {
this.skills = skills;
}
@Override
public String toString() {
return "Skills [skills=" + skills + "]";
}
}
假设我想按年份对我的技能进行分组,这就是我们按年份进行分组的方式:groupBy
public static void main(String[] args) {
List<Skills> skillSet1 = new ArrayList<>();
skillSet1.add(new Skills("Skill-1"));
skillSet1.add(new Skills("Skill-2"));
skillSet1.add(new Skills("Skill-3"));
List<Skills> skillSet2 = new ArrayList<>();
skillSet2.add(new Skills("Skill-1"));
skillSet2.add(new Skills("Skill-4"));
skillSet2.add(new Skills("Skill-2"));
List<Skills> skillSet3 = new ArrayList<>();
skillSet3.add(new Skills("Skill-1"));
skillSet3.add(new Skills("Skill-9"));
skillSet3.add(new Skills("Skill-2"));
List<WorkExperience> workExperienceList = new ArrayList<>();
workExperienceList.add(new WorkExperience(2017,skillSet1));
workExperienceList.add(new WorkExperience(2017,skillSet2));
workExperienceList.add(new WorkExperience(2018,skillSet3));
Map<Integer, Set<List<Skills>>> collect = workExperienceList.stream().collect(
Collectors.groupingBy(
WorkExperience::getYear,
Collectors.mapping(WorkExperience::getSkill, Collectors.toSet())
)
);
}
groupBy
正在返回:
但我需要的是:Map<Integer, Set<List<Skills>>>
Map<Integer, Set<Skills>>
如何将列表流转换为单个容器?