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



我对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]]}

你可以像这样使用 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;
}

希望有帮助。

编辑:

根据霍尔格的评论(要好得多):

    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())));

相关内容

  • 没有找到相关文章

最新更新