Files
openide/java/java-impl/resources/inspectionDescriptions/ArraysAsListWithZeroOrOneArgument.html
Leonid Shalupov 40795fe787 IJI-2422: community/java: move resources under resources root
GitOrigin-RevId: 8b2b63fc6db476ca0c2cfe5cadd84db6c4236d0f
2025-02-05 04:43:28 +00:00

27 lines
1.1 KiB
HTML

<html>
<body>
Reports calls to <code>Arrays.asList()</code> with at most one argument.
<p>
Such calls could be replaced
with <code>Collections.singletonList()</code>, <code>Collections.emptyList()</code>,
or <code>List.of()</code> on JDK 9 and later, which will save some memory.</p>
<p>In particular, <code>Collections.emptyList()</code> and <code>List.of()</code> with no arguments
always return a shared instance,
while <code>Arrays.asList()</code> with no arguments creates a new object every time it's called.</p>
<p>Note: the lists returned by <code>Collections.singletonList()</code> and <code>List.of()</code> are immutable,
while the list returned <code>Arrays.asList()</code> allows calling the <code>set()</code> method.
This may break the code in rare cases.</p>
<p><b>Example:</b></p>
<pre><code>
List&lt;String&gt; empty = Arrays.asList();
List&lt;String&gt; one = Arrays.asList("one");
</code></pre>
<p>After the quick-fix is applied:</p>
<pre><code>
List&lt;String&gt; empty = Collections.emptyList();
List&lt;String&gt; one = Collections.singletonList("one");
</code></pre>
<!-- tooltip end -->
<p>
</body>
</html>