转换列表<a> to Map<b,list<a>&gt;

时间:2016-08-05 17:38:51

标签: collections lambda java-8

class Student{
    String name;
    String grade;
    String vocationalSubject;
}

class Vocational{
    String grade;
    String vocationalSubject;
}

I want to convert List<Student> to Map<Vocational,List<Student>> using java 8 lambdas.

If I have Vocational as a member of Student, then I can do like :
students.stream().collect(Collectors.groupingBy(Student::getVocational))

As I do not have Vocational as a member of Student, I need to create it using Vocational::new But I am not sure how to set those two fields(grade and vocationalSubject) from Student in Vocational.

Trying to Do it using Java 8 Lambdas

2 个答案:

答案 0 :(得分:3)

假设您已正确实施equalshashCode方法,则可以在Vocational函数中创建groupingBy个对象。

Map<Vocational, List<Student>> mapping = students.stream()
    .collect(
        Collectors.groupingBy(s -> new Vocational(s.getGrade(), s.getVocationalSubject()))
    );

答案 1 :(得分:1)

可能不完美,但这应该接近你想要的。即使您没有覆盖“等于”方法,这也会有效。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

class Student
{

    String name;
    String grade;
    String vocationalSubject;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    public String getVocationalSubject() {
        return vocationalSubject;
    }

    public void setVocationalSubject(String vocationalSubject) {
        this.vocationalSubject = vocationalSubject;
    }



    Student(String name,String grade,String vocSub)
    {
        this.name=name;
        this.grade=grade;
        this.vocationalSubject=vocSub;
    }

    @Override
    public String toString() {
        return "Student [name=" + name + ", grade=" + grade + ", vocationalSubject=" + vocationalSubject + "]";
    }
}

class Vocational
{
    String grade;
    String vocationalSubject;

    public Vocational(String grade, String vocationalSubject) {
        this.grade = grade;
        this.vocationalSubject = vocationalSubject;
    }

    @Override
    public String toString() {
        return "Vocational [grade=" + grade + ", vocationalSubject=" + vocationalSubject + "]";
    }

}

public class Temp 
{

    public static void main(String args[]){
        Map<Vocational,List<Student>> map=new HashMap<Vocational,List<Student>>();
        List<Student> listStu=new ArrayList<Student>();
        listStu.add(new Student("Student1","100","aa"));
        listStu.add(new Student("Student1","100","bb"));
        listStu.add(new Student("Student2","50","aa"));
        listStu.add(new Student("Student2","50","bb"));
        listStu.add(new Student("Student3","100","aa"));
        listStu.add(new Student("Student3","50","bb"));
        listStu.add(new Student("Student4","50","aa"));
        listStu.add(new Student("Student4","100","bb"));

        listStu.stream().collect(Collectors.groupingBy(Student::getGrade,Collectors.groupingBy(Student::getVocationalSubject)))
        .forEach((k,v)->v.forEach((x,y)->map.put(new Vocational(k, x), y)));

        map.forEach((x,y)->System.out.println(x+":"+y));
    }


}