Working with Collections Framework

1. Understanding Collection Interface Hierarchy

Collection hierarchy

Iterable
└── Collection
    ├── List         (ordered, indexed, duplicates)
    │   ├── ArrayList, LinkedList, Vector, Stack
    ├── Set          (no duplicates)
    │   ├── HashSet, LinkedHashSet, TreeSet (SortedSet/NavigableSet)
    └── Queue
        ├── PriorityQueue, ArrayDeque
        └── Deque (double-ended)

Map  (not a Collection)
├── HashMap, LinkedHashMap, TreeMap, Hashtable, IdentityHashMap
InterfaceKey Operations
Collectionadd, remove, contains, size, iterator
Listget(i), set(i,e), indexOf
SetUniqueness via equals/hashCode
Mapput, get, containsKey, keySet, values, entrySet

2. Using ArrayList

MethodComplexity
get(i) / set(i,e)O(1)
add(e) endO(1) amortized
add(i,e) / remove(i)O(n)
contains(o)O(n)
CapacityBacking array, grows by ~50%

3. Using LinkedList

MethodComplexity
get(i)O(n)
addFirst/addLastO(1)
remove(iterator)O(1)
ImplementsList, Deque
Note: ArrayList outperforms LinkedList in most real workloads due to cache locality.

4. Using Vector LEGACY

AspectDetail
Thread-safeAll methods synchronized
Modern alternativeCollections.synchronizedList(...) or CopyOnWriteArrayList

5. Using Stack LEGACY

AspectDetail
Methodspush, pop, peek, empty, search
Modern alternativeArrayDeque as stack (faster, not synchronized)

6. Choosing List Implementation

NeedChoice
Random access, end-addArrayList
Frequent middle insertLinkedList (rarely worth it)
Stack/Queue/DequeArrayDeque
Thread-safe iterationCopyOnWriteArrayList
ImmutableList.of(...)

7. Using HashSet

PropertyDetail
OrderNone (hash order)
NullOne null allowed
ComplexityO(1) average for add/contains/remove
BackingHashMap

8. Using LinkedHashSet

PropertyDetail
OrderInsertion order
PerformanceSlightly slower than HashSet
Use casePredictable iteration + uniqueness

9. Using TreeSet

PropertyDetail
OrderNatural or comparator
ComplexityO(log n)
ImplementsNavigableSet
Methodsfirst, last, higher, lower, floor, ceiling, subSet
BackingRed-black tree

10. Using HashMap

Example: HashMap operations

Map<String, Integer> counts = new HashMap<>();
counts.merge("apple", 1, Integer::sum);
counts.computeIfAbsent("user", k -> loadCount(k));
counts.forEach((k, v) -> System.out.println(k + "=" + v));
MethodUse
put(k,v) / get(k)O(1) avg
putIfAbsent(k,v)Only if missing
computeIfAbsent(k, fn)Lazy init
compute(k, biFn)Update with old value
merge(k, v, biFn)Combine if exists
getOrDefault(k, def)Default value
NullOne null key, multiple null values

11. Using LinkedHashMap

PropertyDetail
OrderInsertion (or access if configured)
LRU cacheOverride removeEldestEntry()

12. Using TreeMap

MethodUse
firstKey/lastKeyEndpoints
floorKey/ceilingKey≤ / ≥ key
headMap/tailMap/subMapRange views
ComplexityO(log n)

13. Using Hashtable LEGACY

AspectDetail
Thread-safeAll methods synchronized
NullForbidden
Modern alternativeConcurrentHashMap

14. Understanding IdentityHashMap

AspectDetail
Equality== instead of equals
HashSystem.identityHashCode(o)
Use caseObject graph traversal, cycle detection