Immutable collection creation can be replaced with collection factory call
Reports java.util.Collections
unmodifiable collection calls that can be converted to newer collection factory methods. These can be replaced with e.g. List.of()
or Set.of()
introduced in Java 9 or List.copyOf()
introduced in Java 10.
Note that in contrast to java.util.Collections
methods, Java 9 collection factory methods:
Do not accept
null
values.Require unique set elements and map keys.
Do not accept
null
arguments to query methods likeList.contains()
orMap.get()
of the collections returned.
When these cases are violated, exceptions are thrown. This can change the semantics of the code after the migration.
Example:
After the quick-fix is applied:
This inspection only reports if the language level of the project or module is 9 or higher.
Use the Do not warn when content is non-constant option to report only in cases when the supplied arguments are compile-time constants. This reduces the chances that the behavior changes, because it's not always possible to statically check whether original elements are unique and not null
.
Use the Suggest 'Map.ofEntries' option to suggest replacing unmodifiable maps with more than 10 entries with Map.ofEntries()
.
New in 2017.2
Inspection options
Option | Type | Default |
---|---|---|
Only report when content is constant | Checkbox | false |
Suggest 'Map.ofEntries()' | Checkbox | true |
Inspection Details | |
---|---|
Available in: | IntelliJ IDEA 2023.3, Qodana for JVM 2023.3 |
Plugin: | Java, 233.SNAPSHOT |