Learn · OOP & Java
Java Collections
Java hands you ready-made containers so you never rebuild a list or a lookup table by hand. Knowing which to pick is half of writing clean code — and a constant interview question.
Before we start
- What a collection is and the four you must know
- List vs Set vs Map vs Queue — and when to reach for each
- Why a Set can’t hold duplicates (the trick underneath)
- How these build directly on Arrays & Hashing
- Pick the right collection for a problem, on purpose
- Explain why a Set removes duplicates automatically
- Use List, Set, Map and Queue in real Java code
Why you’re learning it: collections appear in every Java program and every interview (“List vs Set?”). They’re the practical face of your Arrays & Hashing lesson. ⏱️ ~25 min.
The idea — the right container for the job
You wouldn’t carry soup in a basket or books in a bucket. Data is the same: each collection is a container shaped for a job. Pick by asking two questions — do I need order? and do I find things by position, by uniqueness, or by a key? Here are the four, drawn out:
📋 List — an ordered, numbered shelf
Keeps order, allows duplicates, reach any item by its index. (Like the array you already know — but it grows.)
Note the two As — a List is happy with duplicates.
🚫 Set — a guest list (each name once)
Add the same thing twice and the second is silently ignored. No order, but instant “is this in here?”.
🗺️ Map — labelled drawers (key → value)
Store a value under a key, fetch it instantly by that key. A dictionary; your phone contacts.
🎟️ Queue — a line at the counter (FIFO)
First in, first served. Add at the back, remove from the front.
Choose the right one
| Need | Use | (Java class) | Reach for it when… |
|---|---|---|---|
| List | List | ArrayList | a sequence — a playlist, rows from a query |
| Set | Set | HashSet | no duplicates — unique visitors, “seen before?” |
| Map | Map | HashMap | look up by name/id — user by email |
| Queue | Queue | LinkedList / ArrayDeque | process in order — job/task queues |
The key insight
A Set has no duplicates because underneath it’s a Map using the items themselves as keys — and keys are unique by definition. A HashMap gives O(1) lookups for the exact reason from your hashing lesson (the hash points straight to the slot). So collections aren’t new magic — they’re the data structures you already learned, gift-wrapped.
Where you’ll use it — real life
Holding query rows, config by key, unique ids — daily Java.
Drop a list into a Set and duplicates vanish for free.
A Map turns “find the user with this id” into one call.
Your Relay task-queue project is a Queue in action.
Now YOU do the reps
First, the recognition drill — pick the collection before opening the answer:
You must remove duplicate user-IDs from a list. Which collection?
Set — add them all; duplicates drop automatically.
You need to look up an order by its order-number, fast. Which?
Map — key = order number, value = the order.
You’re processing sign-ups strictly in the order they arrived. Which?
Queue — FIFO, first come first served.
You need the top 10 songs in ranked order, duplicates possible. Which?
List — ordered and duplicates allowed; index = rank.
Then code them (both use collections directly):
Out loud: “When do I use a List vs a Set vs a Map vs a Queue, and why does a Set have no duplicates?” Then log it in your Journal.
Next: Exceptions →