mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 14:23:28 +07:00
41 lines
3.0 KiB
HTML
41 lines
3.0 KiB
HTML
<html>
|
|
<body>
|
|
Reports stream API call chains that can be simplified.
|
|
Simplification will often avoid some temporary object creation during collection traversal.
|
|
<p>
|
|
The inspection replaces the following call chains:
|
|
</p>
|
|
<ul>
|
|
<li><code>collection.stream().forEach()</code> → <code>collection.forEach()</code></li>
|
|
<li><code>collection.stream().collect(toList/toSet/toCollection())</code> → <code>new CollectionType<>(collection)</code></li>
|
|
<li><code>collection.stream().toArray()</code> → <code>collection.toArray()</code></li>
|
|
<li><code>Arrays.asList().stream()</code> → <code>Arrays.stream()</code> or <code>Stream.of()</code></li>
|
|
<li><code>IntStream.range(0, array.length).mapToObj(idx -> array[idx])</code> → <code>Arrays.stream(array)</code></li>
|
|
<li><code>IntStream.range(0, list.size()).mapToObj(idx -> list.get(idx))</code> → <code>list.stream()</code></li>
|
|
<li><code>Collections.singleton().stream()</code> → <code>Stream.of()</code></li>
|
|
<li><code>Collections.emptyList().stream()</code> → <code>Stream.empty()</code></li>
|
|
<li><code>stream.filter().findFirst().isPresent()</code> → <code>stream.anyMatch()</code></li>
|
|
<li><code>stream.collect(counting())</code> → <code>stream.count()</code></li>
|
|
<li><code>stream.collect(maxBy())</code> → <code>stream.max()</code></li>
|
|
<li><code>stream.collect(mapping())</code> → <code>stream.map().collect()</code></li>
|
|
<li><code>stream.collect(reducing())</code> → <code>stream.reduce()</code></li>
|
|
<li><code>stream.collect(summingInt())</code> → <code>stream.mapToInt().sum()</code></li>
|
|
<li><code>stream.mapToObj(x -> x)</code> → <code>stream.boxed()</code></li>
|
|
<li><code>stream.map(x -> {...; return x;})</code> → <code>stream.peek(x -> ...)</code></li>
|
|
<li><code>!stream.anyMatch()</code> → <code>stream.noneMatch()</code></li>
|
|
<li><code>!stream.anyMatch(x -> !(...))</code> → <code>stream.allMatch()</code></li>
|
|
<li><code>stream.map().anyMatch(Boolean::booleanValue)</code> → <code>stream.anyMatch()</code></li>
|
|
<li><code>IntStream.range(expr1, expr2).mapToObj(x -> array[x])</code> → <code>Arrays.stream(array, expr1, expr2)</code></li>
|
|
<li><code>Collection.nCopies(count, ...)</code> → <code>Stream.generate().limit(count)</code></li>
|
|
<li><code>stream.sorted(comparator).findFirst()</code> → <code>Stream.min(comparator)</code></li>
|
|
<li><code>optional.orElseGet(() -> { throw new ...; })</code> → <code>optional.orElseThrow()</code></li>
|
|
</ul>
|
|
<p>
|
|
Note that the replacement semantics may have minor differences in some cases. For example,
|
|
<code>Collections.synchronizedList(...).stream().forEach()</code> is not synchronized while
|
|
<code>Collections.synchronizedList(...).forEach()</code> is synchronized.
|
|
Also, <code>collect(Collectors.maxBy())</code> returns an empty <code>Optional</code> if the resulting element is
|
|
<code>null</code> while <code>Stream.max()</code> throws <code>NullPointerException</code> in this case.
|
|
</p>
|
|
</body>
|
|
</html> |