Java8 Stream API:map,groupingBy到不同的对象

时间:2017-10-10 02:40:23

标签: java-8 java-stream

我是Java8的新手。我需要按照特定条件将普通对象列表转换为组,并在UI上显示。

  • 地图的关键应该是" fromDate-todate"和 学生列表应与此组合相关联。
  • 此外,< ClassOfMsSwan'对象需要转换为'学生'对象并保存到列表中。
  • Java1.7代码已经编写完成并且需要 转换为Java1.8。

请帮助我。

public class ClassOfMsSwan {

private String fromDate;
private String toDate;
private String studentName;
private String subject;
private String marks;

public ClassOfMsSwan(String fromDate, String toDate, String studentName, String subject, String marks) {
    super();
    this.fromDate = fromDate;
    this.toDate = toDate;
    this.studentName = studentName;
    this.subject = subject;
    this.marks = marks;
}
// setters and getters


public class Student {

private String name;
private String subject;
private String marks;
public Student(String name, String subject, String marks) {
    this.marks = marks;
    this.subject = subject;
    this.name = name;
}   
// setters and getters




List<ClassOfMsSwan> classes = new ArrayList<>();

    ClassOfMsSwan classOfMsSwan1 = new ClassOfMsSwan("06/01/2016", "05/31/2017", "David", "Physics", "98");
    ClassOfMsSwan classOfMsSwan2 = new ClassOfMsSwan("06/01/2016", "05/31/2017", "Jane", "Botany", "85");
    ClassOfMsSwan classOfMsSwan3 = new ClassOfMsSwan("06/01/2016", "05/31/2017", "Mathew", "Algebra", "76");

    ClassOfMsSwan classOfMsSwan4 = new ClassOfMsSwan("06/01/2017", "05/31/2018", "David", "Geometry", "94");
    ClassOfMsSwan classOfMsSwan5 = new ClassOfMsSwan("06/01/2017", "05/31/2018", "Jane", "Organic Chemistry", "92");
    ClassOfMsSwan classOfMsSwan6 = new ClassOfMsSwan("06/01/2017", "05/31/2018", "Mathew", "Botany", "100");

    ClassOfMsSwan classOfMsSwan7 = new ClassOfMsSwan("06/01/2018", "05/31/2019", "Mathew", "Geometry", "86");
    ClassOfMsSwan classOfMsSwan8 = new ClassOfMsSwan("06/01/2018", "05/31/2019", "Jane", "Algebra", "84");

    classes.add(classOfMsSwan1);
    classes.add(classOfMsSwan2);
    classes.add(classOfMsSwan3);
    classes.add(classOfMsSwan4);
    classes.add(classOfMsSwan5);
    classes.add(classOfMsSwan6);
    classes.add(classOfMsSwan7);
    classes.add(classOfMsSwan8);

    // JDK1.7
    Map<String, List<Student>> dateStudentMap = new HashMap<>();
    for(ClassOfMsSwan classOfMsSwan : classes) {
        List<Student> students = dateStudentMap.get(classOfMsSwan.getFromDate() + "-" + classOfMsSwan.getToDate());
        if(CollectionUtils.isEmpty(students)) {
            students = new ArrayList<>();
            dateStudentMap.put(classOfMsSwan.getFromDate() + "-" + classOfMsSwan.getToDate(), students);
        }
        students.add(new Student(classOfMsSwan.getStudentName(), classOfMsSwan.getSubject(), classOfMsSwan.getMarks()));
    }


Desired output
   {06/01/2016-05/31/2017=[Student [name=David, subject=Physics, marks=98], Student [name=Jane, subject=Botany, marks=85], Student [name=Mathew, subject=Algebra, marks=76]], 06/01/2017-05/31/2018=[Student [name=David, subject=Geometry, marks=94], Student [name=Jane, subject=Organic Chemistry, marks=92], Student [name=Mathew, subject=Botany, marks=100]], 06/01/2018-05/31/2019=[Student [name=Mathew, subject=Geometry, marks=86], Student [name=Jane, subject=Algebra, marks=84]]}

1 个答案:

答案 0 :(得分:2)

您可以像这样使用Java 8 API:

Map<String, Set<Student>> dateStudentMap = classes.stream().map(e ->  
                new Entry(e.getFromDate() + "-" + e.getToDate(),
                        new Student(e.getStudentName(), e.getSubject(), e.getMarks())
            ))
    .collect(groupingBy(Entry::getKey, TreeMap::new,
            mapping(Entry::getValue, toSet())));

我使用如下定义的原始助手类Entry

class Entry{
    public Entry(String key, Student value){
        this.setKey(key);
        this.setValue(value);
    }
    //getters and setters

    private String key;
    private Student value;
}

希望有所帮助。

修改

听取Holger的评论(好多了):

    Map<String, Set<Student>> dateStudentMap8 = classes.stream()
            .collect(groupingBy(e -> 
                        e.getFromDate() + "-" + e.getToDate(),
                        mapping(e -> 
                            new Student(e.getStudentName(), 
                                        e.getSubject(), 
                                        e.getMarks()), 
                            toSet())));