Nearby lessons
82 of 125Java - Collections Framework
- 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)
Collections Framework is a core concept of the Java language. This lesson explains The Problem Before Collections, Collection vs Collections — Do Not Confuse and The Family Tree in Simple Form with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
The Problem Before Collections
An array has a fixed size. What if you need to store 5 numbers now and 50 later? Arrays cannot grow. Managing such groups by hand was painful.
Java's Collection Framework (in java.util) gives us ready-made, growable, powerful classes to store and manage groups of objects. It also gives sorting, searching, and thread-safe versions — all free.
Think of it as your wardrobe: instead of one fixed-size box, you get many types of cabinets — a shelf (List), a storage bag that rejects duplicates (Set), and a labelled box for name-to-value pairs (Map).
Collection vs Collections — Do Not Confuse
| Name | What it is | Example |
|---|---|---|
| Collection | An interface — the root of the collection family (List, Set, Queue). | Collection<String> c = new ArrayList<>(); |
| Collections | A utility class with static helper methods for sorting, searching, reversing. | Collections.sort(list); |
The Family Tree in Simple Form
Choosing the Right Collection — Quick Guide
| Your need | Choose |
|---|---|
| Growable list with indexes, duplicates OK | ArrayList |
| No duplicates | HashSet |
| No duplicates + sorted order | TreeSet |
| Key-value pairs, fastest | HashMap |
| Key-value pairs, sorted by key | TreeMap |
| Key-value pairs, insertion order | LinkedHashMap |
| Key-value pairs, thread safe (legacy) | Hashtable |
| FIFO processing (queue) | ArrayDeque / PriorityQueue |
| Sorting custom objects | Comparable or Comparator |
Array vs Collection — Question and Answer
The classic material starts the whole chapter with this question: we already have arrays, why do we need collections?
| Point | Array | Collection |
|---|---|---|
| Size | Fixed — cannot grow | Dynamically growable |
| Type of elements | Same type (homogeneous) | Any type (heterogeneous) by default |
| Extra methods | No built-in sort/search | Ready-made: sort, reverse, search |
| Performance | Faster | A little slower (but powerful) |
| Predefined? | A language feature | Classes in java.util |
- 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.