When using the Stream
API, call chains should be simplified as much as possible to improve readability and maintainability.
This rule raises an issue when one of the following substitution can be made:
Original |
Preferred |
stream.collect(counting())
|
stream.count()
|
stream.collect(maxBy(comparator))
|
stream.max(comparator)
|
stream.collect(minBy(comparator))
|
stream.min(comparator)
|
stream.collect(mapping(mapper))
|
stream.map(mapper).collect()
|
stream.collect(reducing(...))
|
stream.reduce(...)
|
stream.collect(summingInt(mapper))
|
stream.mapToInt(mapper).sum()
|
stream.collect(summingLong(mapper))
|
stream.mapToLong(mapper).sum()
|
stream.collect(summingDouble(mapper))
|
stream.mapToDouble(mapper).sum()
|
Noncompliant code example
int count = stream.collect(counting()); // Noncompliant
Compliant solution
int count = stream.count();