The time complexity of method calls on collections is not always obvious. For instance, for most collections the size()
method takes
constant time, but the time required to execute ConcurrentLinkedQueue.size()
is O(n), i.e. directly proportional to the number of
elements in the collection. When the collection is large, this could therefore be an expensive operation.
This rule raises an issue when the following O(n) methods are called outside of constructors on class fields:
ArrayList
contains
remove
LinkedList
get
contains
ConcurrentLinkedQueue
size
contains
ConcurrentLinkedDeque
size
contains
CopyOnWriteArrayList
add
contains
remove
CopyOnWriteArraySet
add
contains
remove
ConcurrentLinkedQueue queue = new ConcurrentLinkedQueue(); //... log.info("Queue contains " + queue.size() + " elements"); // Noncompliant