Inspectopedia
 
2024.3

Call to 'list.containsAll(collection)' may have poor performance

Warning
New
Last modified: 03 December 2024

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:

After the quick-fix is applied:

New in 2022.1