File tree Expand file tree Collapse file tree
src/main/java/io/github/aplotnikov/java_8_misuses/stream 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 ;
2+
3+ import io .github .aplotnikov .java_8_misuses .domain .Client ;
4+
5+ import java .util .List ;
6+ import java .util .concurrent .atomic .AtomicInteger ;
7+
8+ import static io .github .aplotnikov .java_8_misuses .utils .Annotations .Good ;
9+ import static io .github .aplotnikov .java_8_misuses .utils .Annotations .Ugly ;
10+
11+ class AvoidLoopsInStreams {
12+
13+ @ Ugly
14+ class UseExternalCounter {
15+ double countAverageLoansPerClient (List <Client > clients ) {
16+ if (clients .isEmpty ()) {
17+ return 0 ;
18+ }
19+ AtomicInteger totalCount = new AtomicInteger ();
20+ clients .forEach (client -> totalCount .addAndGet (client .getLoans ().size ()));
21+ return totalCount .doubleValue () / clients .size ();
22+ }
23+ }
24+
25+ @ Good
26+ class ApplyMappingsToTargetType {
27+ double countAverageLoansPerClient (List <Client > clients ) {
28+ return clients .stream ()
29+ .mapToDouble (client -> client .getLoans ().size ())
30+ .average ()
31+ .orElse (0 );
32+ }
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments