본문 바로가기

Java

[자바 8] 람다 스트림 예문 PersonService.java (출처: 자바의정석)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.wego.web.person;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.Comparator;
import java.util.IntSummaryStatistics;
import java.util.stream.Collectors;
import  org.springframework.beans.factory.annotation.Autowired;
import  org.springframework.stereotype.Service;
@Service
public class PersonService {
    @Autowired private PersonRepository  personRepository;
    
    // 자바의 정석 847 collect()
    public List<String> namesOfStudents() {
       // 1. 학생의 이름
       return  personRepository.findByRole("student").stream()
               .map(Person::getName)
               .collect(Collectors.toList());
    }
    public Person[] streamToArray() {
       // 2. 스트림을 배열로 전환
       return  personRepository.findByRole("student").stream()
               .toArray(Person[]::new);
    }
    public Map<String, Person> streamToMap()  {
       // 3. 스트림을 맵으로 전환. 학생이름이  key
       return  personRepository.findByRole("student").stream()
               .collect(Collectors.toMap(s->s.getName(),  p->p));
    }
    public Long theNumberOfStudents() {
       // 4. 학생의 수
       return  personRepository.findByRole("student").stream()
               .collect(Collectors.counting());
    }
    public Integer totalScore() {
       // 5. 학생들 총점
       return  personRepository.findByRole("student").stream()
               .collect(Collectors.summingInt(Person::getScore));
    }
    public Optional<Person> topStudent(){
       // 6. 1등 학생
       return  personRepository.findByRole("student").stream()
               .collect(Collectors.maxBy(Comparator.comparingInt(Person::getScore)));
    }
    public IntSummaryStatistics getStat() {
       // 7. 전체 학생 성적 통계
       return  personRepository.findByRole("student").stream()
               .collect(Collectors.summarizingInt(Person::getScore));
    }
    public String nameList() {
       // 8. 전체 학생 이름
       return  personRepository.findByRole("student").stream()
               .map(Person::getName)
               .collect(Collectors.joining(",","{","}"));
    }
    
    
    // 자바의정석 851 partioningBy()
    public List<Person>  partioningByGender(boolean gender) {
       // 1.단순분할 (성별로 분할) 남성 true  여성 false
       return  personRepository.findByRole("student").stream()
               .collect(
                      Collectors
                          .partitioningBy(Person::isMale))
               .get(gender);
    }
    public Long  partioningCountPerGender(boolean gender) {
       // 2.단순분할 (성별 학생수)
       return  personRepository.findByRole("student").stream()
               .collect(
                      Collectors
                          .partitioningBy(Person::isMale,
                              Collectors.counting()))
               .get(gender);
    }
    public Person  partioningTopPerGender(boolean gender) {
       // 3.단순분할 (성별 1등) 남성 true  여성 false
       return  personRepository.findByRole("student").stream()
               .collect(Collectors.partitioningBy(
                      Person::isMale,
                      Collectors.collectingAndThen(
                              Collectors.maxBy(
                                     Comparator.comparingInt(
                                             Person::getScore)),
                                             Optional::get)))
               .get(gender);
    }
    public List<Person>  partioningRejectPerGender(boolean gender) {
       // 4.다중분할 (성별 불합격자,  50점이하) 남성 true 여성 false
       return  personRepository.findByRole("student").stream()
               .collect(Collectors.partitioningBy(Person::isMale,
                      Collectors.partitioningBy(s->s.getScore()<=50)))
               .get(gender)
               .get(true);
    }
    // 자바의정석 855
    public Map<Integer, List<Person>>  groupingByBan() {
       //1. 단순 그룹화(반별로 그룹화)
        return personRepository
               .findByRole("student").stream()
               .collect(Collectors.groupingBy(Person::getBan));
    }
    public Map<Person.Level, List<Person>>  groupingByGrade() {
       //2. 단순그룹화(성적별로 그룹화)
       return  personRepository.findByRole("student").stream()
               .collect(Collectors.groupingBy(
                      s->{
                          if(s.getScore()>=  80return Person.Level.HIGH;
                          else  if(s.getScore()>=50)return Person.Level.MID;
                          else return  Person.Level.LOW;}
                      ));
       
    }
    public Map<Person.Level, Long>  groupingByCountByLevel() {
       //3. 단순그룹화 + 통계(성적별 학생수))
       return  personRepository.findByRole("student").stream()
               .collect(Collectors.groupingBy(s->{
                  if(s.getScore()>= 80)  return Person.Level.HIGH;
                  else  if(s.getScore()>=50)return Person.Level.MID;
                  else                  return Person.Level.LOW;},
                      Collectors.counting()
               ));
               
               
    }
    public Map<Integer, Map<Integer,  List<Person>>> groupingByHakAndBan() {
       // 4. 다중 그룹화 (학년별, 반별)
       return  personRepository.findByRole("student").stream()
               .collect(
                   Collectors.groupingBy(Person::getHak,
                          Collectors.groupingBy(Person::getBan)
               ));
    }
    public Map<Integer, Map<Integer,  Object>> groupingTopByHakAndBan() {
       // 5. 다중 그룹화 +통계 (학년별, 반별  1등)
       return  personRepository.findByRole("student").stream()
               .collect(Collectors.groupingBy(Person::getHak,
                      Collectors.groupingBy(Person::getBan,
                              Collectors.collectingAndThen(
                              Collectors.maxBy(Comparator
                                     .comparingInt(Person::getScore)),
                              Optional::get))));
    }
    public Map<Object, Set<Object>>  groupingByStat() {
       // 6. 다중그룹화 + 통계(학년별, 반별  성적그룹)
       return  personRepository.findByRole("student").stream()
               .collect(Collectors.groupingBy(s->  s.getHak()+"-"+ s.getBan(),
                      Collectors.mapping(s->{
                          if(s.getScore()>=  80return Person.Level.HIGH;
                          else  if(s.getScore()>=50)return Person.Level.MID;
                          else                  return Person.Level.LOW;},
                          Collectors.toSet())));
                      
    }
}
cs