mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-07 07:55:31 +07:00
GitOrigin-RevId: 8b2b63fc6db476ca0c2cfe5cadd84db6c4236d0f
22 lines
1.4 KiB
HTML
22 lines
1.4 KiB
HTML
<html>
|
|
<body>
|
|
Reports Stream API call chains ending with a <code>count()</code> operation, that are optimizable.
|
|
<p>
|
|
The following call chains can be replaced by this inspection:
|
|
</p>
|
|
<ul>
|
|
<li><code>Collection.stream().count()</code> → <code>Collection.size()</code>. In Java 8 <code>Collection.stream().count()</code>
|
|
actually iterates over the collection elements to count them, while <code>Collection.size()</code> is much faster for most of the collections.</li>
|
|
<li><code>Stream.flatMap(Collection::stream).count()</code> → <code>Stream.mapToLong(Collection::size).sum()</code>. Similarly,
|
|
there's no need to iterate over all the nested collections. Instead, their sizes could be summed up.</li>
|
|
<li><code>Stream.filter(o -> ...).count() > 0</code> → <code>Stream.anyMatch(o -> ...)</code>. Unlike the original call,
|
|
<code>anyMatch()</code> may stop the computation as soon as a matching element is found.</li>
|
|
<li><code>Stream.filter(o -> ...).count() == 0</code> → <code>Stream.noneMatch(o -> ...)</code>. Similar to the above.</li>
|
|
</ul>
|
|
<p>
|
|
Note that if the replacement involves a short-circuiting operation like <code>anyMatch()</code>, there could be a visible behavior change,
|
|
if the intermediate stream operations produce side effects. In general, side effects should be avoided in Stream API calls.
|
|
</p>
|
|
<!-- tooltip end -->
|
|
</body>
|
|
</html> |