The Collections Framework (`java.util`) provides reusable data structures. Master the core interfaces and performance trade-offs.
| Interface | Duplicates Allowed? | Ordered / Indexed? | Key Implementation Classes |
|---|---|---|---|
List<E> |
✅ Yes | ✅ Ordered by insertion index | `ArrayList`, `LinkedList`, `Vector` |
Set<E> |
❌ No (Unique elements only) | ❌ Unordered (`HashSet`) / Sorted (`TreeSet`) | `HashSet`, `TreeSet`, `LinkedHashSet` |
Map<K,V> |
Keys: ❌ No, Values: ✅ Yes | Key-Value pair lookup | `HashMap`, `TreeMap`, `Hashtable` |
| Feature | ArrayList | LinkedList |
|---|---|---|
| Underlying Data Structure | Resizable Dynamic Array. | Doubly Linked List (Nodes with pointers). |
Random Access (get(index)) |
Fast $O(1)$ constant time. | Slow $O(n)$ traversal. |
| Insertion / Deletion | Slow $O(n)$ (requires shifting elements). | Fast $O(1)$ (just updates node pointers). |
| Memory Overhead | Low (only stores elements). | Higher (stores element + next & prev pointers). |
Generics allow classes, interfaces, and methods to operate on specified data types while providing compile-time type safety.
ClassCastException risk.List<String> list = new ArrayList<>(); Compiler catches type mismatches at compile-time!Q: Is Map a child interface of Collection in Java?
Map<K,V> is a standalone interface in the Java Collections Framework because it deals with Key-Value pairs rather than single element collections.