There is this method Collections.sort()
which internally calls Arrays.sort()
. This uses the merge sort to sort data which has the order O(nlogn). And one more information is, if the number of elements to be sorted is lesser than 7, it uses the insertion sort.
As with Vector
and ArrayList
following are the complexities since it uses a simple array
- get(index) - O(1)
- add(Object) - O(n)
- insertAt(int pos, Object value) - O(n)
- remove(Object) - O(n)
One more thing is that HashSet
internally uses a HashMap
only worrying about the keys of a map and the objects being ignored or unused so that there are no two different implementations. And HashMap uses the collision free or the ideal hashing by generating unique hashcodes for every object. So, the order would be ideally 1 for insertion and retrieval of data.
Often in Java Collections, especially in Lists, there is this usual comparison between its two concrete implementations, the ArrayList
and LinkedList
. The order of the LinkedList are as follows.
- get(index) - O(n)
- add(Object) - O(1) (provided a readymade last pointer is maintained in the linked list)
- insertAt(int pos, Object value) - O(n)
- remove(Object) - O(n)