A collection is an object that holds other objects. Instead of making 20 separate variables for 20 students, you put them all in one list. Every language has them β Python has lists, sets, and dicts; Java has ArrayList, HashSet, and HashMap.
Different collections solve different problems. Pick the wrong one and everything is slow or buggy. Pick the right one and your code feels like magic.
| Collection | Ordered? | Duplicates? | Best For |
|---|---|---|---|
| π List | Yes | Yes | Ordered items β a toβdo list |
| π¨ Set | No | No | Unique items β tags, IDs |
| π Map | Key order varies | No duplicate keys | Lookups β name β score |
| πΆ Queue | Yes (FIFO) | Yes | Firstβinβfirstβout β a printer queue |
Ask three questions before picking a collection:
1. Do I need order? If yes β list or queue. If no β set or map.
2. Do I need to prevent duplicates? If yes β set or map keys.
3. Do I need fast lookups by a key? If yes β map.
There's no "best" collection β only the right fit for the question. Think about the question first, then the collection.
Iteration means stepping through items in a collection one at a time. Every language has a forβeach or forβin loop that hides the index details:
for student in students: β clean and readable. Under the hood, an iterator is doing the work: it knows how to point at the "next" item until there are no more.
Never manually track indexes unless you need to. Iterators are less buggy and easier to swap between collection types.
Sorting a list of numbers is easy β but what does it mean to sort a list of Student objects? By name? By grade? By age? You have to decide.
Most languages let you implement a comparison method (compareTo(), __lt__, or a key function) so the sort knows what to compare. You can also give the sort a custom key on the fly.
Not all operations are equally fast. Time complexity describes how an operation's speed changes as your data grows.
O(1) β instant, no matter the size (map lookup by key). O(n) β you touch every item once (loop through a list). O(nΒ²) β a loop inside a loop (bruteβforce duplicate finding).
You don't need to memorize everything. Just know: maps are fast for lookup, lists are fast for iteration, and nested loops can blow up with big data.
Sometimes the builtβin collections don't fit. That's when you build your own β a class that wraps a list, enforces rules, and exposes only the methods you want.
Example: a LimitedStack class that allows at most 10 items and refuses to exceed it. Or a StudentRoster class with a method addStudent() that checks for duplicate IDs. Your class becomes the "smart container" for your domain.
Collection speed test: Create a list of 10,000 student names and a set of the same names. Check if "Alex" is in each β time both. Notice the difference between O(n) and O(1).
Right tool for the job: For three scenarios (a shopping cart, a list of unique email addresses, a phonebook), pick the best collection and explain why in one sentence each.
Iterator walk: Write a loop that goes through a list of books and prints each title. Then rewrite it using an explicit iterator (like iter() / next() in Python, or Iterator in Java). Compare readability.
Sort challenge: Create a Student class with name, age, and grade. Sort a list of students first by grade (highest first), then by name alphabetically. Try using a key function or a compareTo() method.
Map detective: Build a dictionary mapping country names β capitals. Add 5 countries, look up 3 of them, and count how many you know. Bonus: print the capital of a country using .get() so missing countries don't crash.
Custom container: Build a FixedStack class that holds at most 3 items. If someone tries to add a fourth, raise an error. Test with three items (works) and a fourth (error).
By the end of this module, you'll be able to: pick the right collection for any task, iterate cleanly with forβeach loops, sort your own objects, understand basic time complexity, and build small custom collection classes.