Javaの8ストリームとラムダ:属性と異なるオブジェクトリストに設定された他の残りの属性によって、オブジェクトのグループ一覧

クレジット:

Java8ストリーム/ラムダに非常に新しいです。私は学生のリストは、その後、グループ化された学生の属性に基づいてオブジェクト(StudentSubject)を作成する属性(ID)を使用して、オブジェクトのグループにする必要があります。たとえば、私は、次のクラスがあります。

class Student{
        int id;
        String name;
        String subject;
        int score;

        public Student(int id, String name, String subject, int score) {
            this.id = id;
            this.name = name;
            this.subject = subject;
            this.score = score;
        }
}


class StudentSubject {
            String name;
            List<Result> results;
    }   



  class Result {
            String subjectName;
            int score;
    //getter setters
    }

入力を次のためにそう。

id,name,subject,score  
1,John,Math,80
1,John,Physics,65
2,Thomas,Computer,55
2,Thomas,Biology,70

結果出力は次のようになりますList<StudentSubject>から設定した属性を持ちますList<Student>何かを言います:

1=[
            name = 'John'
            Result{subject='Math', score=80}, 
            Result{subject='Physics', score=65}, 
        ]
2=[
            name = 'Thomas'
            Result{subject='Computer', score=55}, 
            Result{subject='Biology', score=70}, 
        ]

どのように私は結果とStudentSubjectするIdを最初に設定した属性によってグループ化するのですか?

     public static void main(String[] args) {
            List<Student> studentList = Lists.newArrayList();
            studentList.add(new Student(1,"John","Math",80));
            studentList.add(new Student(1,"John","Physics",65));
            studentList.add(new Student(2,"Thomas","Computer",55));
            studentList.add(new Student(2,"Thomas","Biology",70));

// group by Id             studentList.stream().collect(Collectors.groupingBy(Student::getId));
//set result list
List<Result> resultList = studentList.stream().map(s -> {
            Result result = new Result();
            result.setSubjectName(s.getSubject());
            result.setScore(s.getScore());
            return result;

        }).collect(Collectors.toList());
    }
YCF_L:

結果はする必要がありますList<StudentSubject>あなたがこの方法を使用することができ、あなたのケースで:

List<StudentSubject> resultList = studentList.stream()
        .collect(Collectors.groupingBy(Student::getId)) // until this point group by Id
        .entrySet()                                     // for each entry
        .stream()                                        
        .map(s -> new StudentSubject(                   // create a new StudentSubject
                s.getValue().get(0).getName(),          // you can use just the name of first element
                s.getValue().stream()                   // with a list of Result
                        .map(r -> new Result(r.getSubject(), r.getScore()))
                        .collect(Collectors.toList()))
        ).collect(Collectors.toList());                 // then collect every thing as a List

アウトプット

StudentSubject{name='John', results=[Result{subjectName='Math', score=80}, Result{subjectName='Physics', score=65}]}
StudentSubject{name='Thomas', results=[Result{subjectName='Computer', score=55}, Result{subjectName='Biology', score=70}]}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=204751&siteId=1
おすすめ