Career OS

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 you’ll learn
  • 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
✅ After this you’ll be able to
  • 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.)

A0
B1
A2
C3

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?”.

ABC ← add “A” again → A

🗺️ Map — labelled drawers (key → value)

Store a value under a key, fetch it instantly by that key. A dictionary; your phone contacts.

"d@x.com"User #123
"a@x.com"User #124

🎟️ Queue — a line at the counter (FIFO)

First in, first served. Add at the back, remove from the front.

front → job 1job 2job 3 ← back

Choose the right one

NeedUse(Java class)Reach for it when…
ListListArrayLista sequence — a playlist, rows from a query
SetSetHashSetno duplicates — unique visitors, “seen before?”
MapMapHashMaplook up by name/id — user by email
QueueQueueLinkedList / ArrayDequeprocess 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

🧾 Everywhere

Holding query rows, config by key, unique ids — daily Java.

🚫 Dedup

Drop a list into a Set and duplicates vanish for free.

🔎 Lookups

A Map turns “find the user with this id” into one call.

📥 Job queues

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):

🗣️ The 2-minute explain test

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 →

Saves your progress on this device.