File tree Expand file tree Collapse file tree
src/main/java/io/github/aplotnikov/java_8_misuses/stream/collectors Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package io .github .aplotnikov .java_8_misuses .stream .collectors ;
2+
3+ import io .github .aplotnikov .java_8_misuses .domain .Client ;
4+
5+ import java .util .IntSummaryStatistics ;
6+ import java .util .List ;
7+ import java .util .stream .IntStream ;
8+
9+ import static io .github .aplotnikov .java_8_misuses .utils .Annotations .Good ;
10+ import static io .github .aplotnikov .java_8_misuses .utils .Annotations .Ugly ;
11+ import static java .util .stream .Collectors .summarizingInt ;
12+
13+ class StatisticsCalculation {
14+
15+ @ Ugly
16+ class IterateThroughValuesSeveralTimes {
17+ void findMinAndMaxClientAge (List <Client > clients ) {
18+ getClientAgesStream (clients )
19+ .max ()
20+ .ifPresent (max -> System .out .println ("MAX: " + max ));
21+
22+ getClientAgesStream (clients )
23+ .min ()
24+ .ifPresent (min -> System .out .println ("MIN: " + min ));
25+ }
26+
27+ private IntStream getClientAgesStream (List <Client > clients ) {
28+ return clients .stream ().mapToInt (Client ::getAge );
29+ }
30+ }
31+
32+ @ Good
33+ class CalculateStatisticsInSingleRunWithCollector {
34+ void findMinAndMaxClientAge (List <Client > clients ) {
35+ IntSummaryStatistics statistics = clients .stream ().collect (summarizingInt (Client ::getAge ));
36+ System .out .println ("MAX: " + statistics .getMax ());
37+ System .out .println ("MIN: " + statistics .getMin ());
38+ }
39+ }
40+ }
You can’t perform that action at this time.
0 commit comments