Table of Contents
1. Collection Hierarchy
The Java Collection Framework provides a well-designed set of interfaces and classes to handle groups of objects. Here is a detailed look at the hierarchy:
- Collection Interface: The root interface in the collection hierarchy.
- Methods:
add(E e)
, remove(Object o)
, size()
, clear()
, contains(Object o)
, iterator()
, etc.
- List Interface: Extends Collection. Ordered collection (sequence) that allows duplicates.
- ArrayList:
- Backed by a dynamic array.
- Provides fast random access.
- Slower for insertion and deletion in the middle.
- LinkedList:
- Doubly-linked list implementation.
- Efficient for insertions and deletions.
- Can act as both a list and a queue.
- Vector:
- Synchronized, resizable array.
- Legacy class; similar to ArrayList but thread-safe.
- Stack:
- Extends Vector.
- Implements LIFO (Last-In-First-Out) stack.
- Set Interface: Extends Collection. No duplicate elements allowed.
- HashSet:
- Backed by a hash table.
- Unordered.
- LinkedHashSet:
- HashSet with predictable iteration order.
- TreeSet:
- Implements NavigableSet, backed by a TreeMap.
- Elements are sorted.
- Queue Interface: Extends Collection. Used to hold multiple elements prior to processing.
- PriorityQueue:
- Unbounded priority queue based on a priority heap.
- Elements are ordered by natural ordering or by a Comparator provided at queue construction time.
- LinkedList:
- Map Interface: Not a true collection but part of the framework. Maps keys to values.
- HashMap:
- Backed by a hash table.
- Allows null values.
- LinkedHashMap:
- HashMap with predictable iteration order.
- TreeMap:
- Sorted map backed by a TreeMap.
- Hashtable:
- Synchronized map.
- Legacy class.
2. List Interface and Its Implementations
- ArrayList:
- Structure: Uses a dynamic array to store elements.
- Advantages:
- Fast random access (
get
and set
operations).
- Automatically resizes when more elements are added.
- Disadvantages:
- Slower for insertion and deletion in the middle (due to shifting elements).
- LinkedList:
- Structure: Uses a doubly-linked list.
- Advantages:
- Efficient for insertion and deletion operations.
- Can be used as both a list and a queue.
- Disadvantages:
- Slower for random access compared to ArrayList.
- Vector:
- Structure: Similar to ArrayList but synchronized.
- Advantages:
- Disadvantages:
- Generally slower due to synchronization overhead.
- Legacy class; use ArrayList for unsynchronized needs.
- Stack:
- Structure: Extends Vector to provide stack operations.
- Methods:
push()
, pop()
, peek()
, isEmpty()
.
- Use Case: LIFO stack operations.
3. Iterator and Iterable Interfaces
- Iterable Interface:
- Purpose: To be implemented by any class whose objects can be iterated.
- Method:
iterator()
, which returns an Iterator.
- Iterator Interface:
- Purpose: To traverse elements in a collection.
- Methods:
boolean hasNext()
: Returns true if the iteration has more elements.
E next()
: Returns the next element in the iteration.
void remove()
: Removes from the underlying collection the last element returned by this iterator (optional operation).
4. Implementing Iterable via Custom LinkedList