Collection Framework
Array List
A resizable array. Ordered. Indexed.
Key traits
Maintains insertion order
Allows duplicates
Allows multiple
nullFast random access
Slow middle insert/delete
HashSet
A set backed by HashMap.
Key traits
No duplicates
No order guarantee
Allows one
nullFast operations
O(1)Depends on
hashCode()andequals()
TreeSet
A sorted set backed by a Red-Black Tree.
Key traits
No duplicates
Always sorted
No
nullO(log n)operationsUses
ComparableorComparator
HashMap
A hash table-based key–value store.
Key traits
Keys are unique
Values can repeat
One
nullkey allowedMultiple
nullvalues allowedAverage
O(1)
TreeMap
A sorted map backed by a Red-Black Tree.
Key traits
Keys are sorted
No
nullkeysValues can be
nullO(log n)Uses
ComparableorComparator
# Summary
Collection | Order | Duplicates | Nulls | Complexity |
|---|---|---|---|---|
ArrayList | Yes | Yes | Yes | Access O(1) |
HashSet | No | No | One | O(1) |
TreeSet | Sorted | No | No | O(log n) |
HashMap | No | Key: No | 1 key | O(1) |
TreeMap | Sorted | Key: No | No key | O(log n) |
Sorting
Collections.sort()
Features:
Sorts in-place
Uses TimSort
TimSort:
Hybrid of merge sort + insertion sort
Stable
Optimized for partially sorted data
Time Complexity: O(n log n) Space Complexity: O(log n)
Reverse
Min and Max
Time Complexity: O(n)
Frequency
Time Complexity: O(n)