I am trying to figure out what are the complexities (using the big-o notation) of operations on Java basic collections. Similarly as in this question for C++ language.
Here is what I have, using the common sense:
Lists
ArrayList
- add(E e) O(1)
- add(int index, E e) O(n)
- set(int index, E element O(1)
- get(int index) O(1)
- remove(E e) remove(int index) O(n)
- contains(E e) O(n)
- list concatenation (addAll) O(n)
LinkedList
- add(E e) O(1)
- add(int index, E e) O(n)
- set(int index, E element O(n)
- get(int index) O(n)
- remove(E e) remove(int index) O(n)
- contains(E e) O(n)
- list concatenation (addAll) O(1)
Sets
HashSet and LinkedHashSet
- add(E e) O(1)
- remove(E e) O(1)
- contains(E e) O(1)
- find/get an element using iterator O(n)
TreeSet
- add(E e) O(log n)
- remove(E e) O(log n)
- contains(E e) O(log n)
- addAll O(n log n)
- find an element using iterator O(n) or perhaps O(log n) when implementing the binary search
Maps
HashMap and LinkedHashMap
- put(K key, V value) O(1)
- get(K key) O(1)
- remove(E e) O(1)
- containsKey(Object key) O(1)
- containsValue(Object value) O(n)
- putAll O(n)
- find/get an element using iterator O(n) or perhaps O(log n) same as in
TreeSet
TreeMap
- put(K key, V value) O(log n)
- get(K key) O(log n)
- remove(E e) O(log n)
- containsKey(Object key) O(log n)
- containsValue(Object value) O(n)
- putAll O(n log n)
- find/get an element using iterator O(n)
NOTE: Collections based on hashing assume well designed hash function to be in O(1) otherwise it is in O(n)
The Question: Is this correct?