Nearby lessons
83 of 125Java - List Interface
📌 What You Will Learn
- What the Collection Framework is and why we need it
- List, Set, Map and Queue — the four big families
- Choosing the right collection for the right job
- Comparable vs Comparator
- The internal working of HashMap (interview favourite)
List Interface is a core concept of the Java language. This lesson explains List — Ordered, Allows Duplicates and add() vs addAll() with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
List — Ordered, Allows Duplicates
A List keeps values in insertion order and allows duplicates. Each element has an index (position), like an array that can grow.
ArrayList vs LinkedList vs Vector
| Point | ArrayList | LinkedList | Vector |
|---|---|---|---|
| Structure | Resizable array | Doubly linked list | Like ArrayList but old |
| Getting by index | Very fast (O(1)) | Slow (O(n)) | Fast |
| Adding/removing in middle | Slow (shifting) | Fast | Slow |
| Thread safe? | No | No | Yes (synchronized) |
| When to use | Most common choice | Frequent insert/delete in middle | Legacy code (avoid for new work) |
Example01
add() vs addAll()
- `add(element)` — adds ONE element.
- `addAll(collection)` — adds ALL elements of another collection at once. Returns true if the collection changed.
Example02
📝 Key Takeaways
- Collection is an interface; Collections is a utility class with static helpers.
- List = ordered, allows duplicates. Set = no duplicates. Map = key-value pairs. Queue = FIFO.
- ArrayList is the everyday list; HashMap is the everyday map.
- HashMap works on hashCode + buckets; collisions chain, then become trees (Java 8+).
- Comparable (compareTo) is the natural order; Comparator (compare) gives custom order.
- Always override equals() and hashCode() together for your own objects.
- Collections.sort() and Collections.reverse() are the handy helpers.
🧠 Test Your Knowledge
1 QuestionsProgress: 0 / 1