count() operation that
could be optimized.
The following call chains are replaced by this inspection:
Collection.stream().count() → Collection.size(). In Java 8 Collection.stream().count()
actually iterates over collection elements to count them while Collection.size() is much faster for most of collections.Stream.flatMap(Collection::stream).count() → Stream.mapToLong(Collection::size).sum(). Similarly
there's no need to iterate all the nested collections. Instead, their sizes could be summed up.Stream().filter(o -> ...).count() > 0 → Stream().anyMatch(o -> ...). Unlike the original call,
anyMatch() may stop the computation as soon as an element is found.Stream().filter(o -> ...).count() == 0 → Stream().noneMatch(o -> ...). Similar to above.
Note that if the replacement involves short-circuiting operation like anyMatch() there could be visible behavior change,
if the intermediate stream operations produce side effects. In general, side-effects should be avoided in Stream API calls.
New in 2016.3