Call to 'list.containsAll(collection)' may have poor performance
Reports calls to containsAll()
on java.util.List
.
The time complexity of this method call is O(n·m), where n is the number of elements in the list on which the method is called, and m is the number of elements in the collection passed to the method as a parameter. When the list is large, this can be an expensive operation.
The quick-fix wraps the list in new java.util.HashSet<>()
since the time required to create java.util.HashSet
from java.util.List
and execute containsAll()
on java.util.HashSet
is O(n+m).
Example:
public boolean check(List<String> list, Collection<String> collection) {
// O(n·m) complexity
return list.containsAll(collection);
}
After the quick-fix is applied:
public boolean check(List<String> list, Collection<String> collection) {
// O(n+m) complexity
return new HashSet<>(list).containsAll(collection);
}
New in 2022.1
Inspection Details | |
---|---|
Available in: | IntelliJ IDEA 2023.3, Qodana for JVM 2023.3 |
Plugin: | Java, 233.SNAPSHOT |
Last modified: 13 July 2023