There should be a guide for other methods - just think about how an effective implementation should look.
Most of the other operations with arrays in collections (operations that process each element in the collection) are O(n)
, so they are not mentioned there. Examples are filter
, map
, foreach
, indexOf
, reverse
, find
...
Methods that return iterators or streams, such as combinations
and permutations
, are usually O(1)
.
Methods involving 2 collections, usually O(max(n, m))
or O(min(n, m))
. This is zip
, zipAll
, sameElements
, corresponds
, ...
The union
, diff
and intersect
methods are O(n + m)
.
Sorting options, naturally, O(nlogn)
. groupBy
O(nlogn)
in the current implementation. indexOfSlice
uses the KMP and O(m + n)
algorithm, where m
and n
are the string lengths.
Methods such as +:
, :+
or patch
, usually O(n)
, if you are not dealing with a specific case of an immutable collection for which this operation is more efficient - for example, adding an element to a functional List
or adding an element to Vector
.
toX
methods toX
usually O(n)
, since they must toX
over all the elements and create a new collection. The exception is toStream
, which builds the collection lazily - therefore it is O(1)
. Also, whenever X
is a type of the toX
collection, this
returned, being O(1)
.
Iterator implementations must have O(1)
(amortized) next
and hasNext
. Creating an iterator should be the worst O(logn)
, but O(1)
in most cases.