📊 Java Collections Time Complexity
Table
Collection Read Write Time 🚀 Why this Complexity?
Time
ArrayList O(1) (get) O(1) for add at Uses indexed array; direct access is
end, O(n) for fast, but inserting/deleting shifts
insert/delete elements.
LinkedList O(n) O(1) at head/tail No indexing; access needs traversal,
but adding/removing nodes is quick at
ends.
HashMap O(1) avg O(1) avg Uses hashing; collisions handled by
O(n) O(n) worst chaining/tree, which can degrade
worst performance.
TreeMap O(log n) O(log n) Internally uses Red-Black Tree
(balanced BST); read/write require
log(n) operations.
HashSet O(1) avg O(1) avg Backed by HashMap; stores values as
O(n) O(n) worst keys with dummy values. Same
worst performance as HashMap.
TreeSet O(log n) O(log n) Backed by TreeMap (Red-Black Tree);
keeps sorted order, hence log(n)
complexity.