Inspectopedia Help

Call to 'Arrays.asList()' with too few arguments

Reports calls to Arrays.asList() with at most one argument.

Such calls could be replaced with Collections.singletonList(), Collections.emptyList(), or List.of() on JDK 9 and later, which will save some memory.

In particular, Collections.emptyList() and List.of() with no arguments always return a shared instance, while Arrays.asList() with no arguments creates a new object every time it's called.

Note: the lists returned by Collections.singletonList() and List.of() are immutable, while the list returned Arrays.asList() allows calling the set() method. This may break the code in rare cases.

Example:

List<String> empty = Arrays.asList(); List<String> one = Arrays.asList("one");
List<String> empty = Collections.emptyList(); List<String> one = Collections.singletonList("one");

Inspection Details

By default bundled with:

IntelliJ IDEA 2024.1, Qodana for JVM 2024.1,

Can be installed with plugin:

Java, 241.14841

Last modified: 12 March 2024