Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
← Fundamentals of Object Oriented Programming Level Two Β· Lesson 3 of 8

Module Two

πŸ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
3
Module 2: Collections, Iteration & Data Structures for Young Coders
⭐ Module 2 · Level Two

Collections, Iteration
& Data Structures

πŸ“¦ Store, find, and organize objects like a pro

🎯 What you'll learn in this module

  • βœ” How lists, sets, maps, and queues work in OOP languages
  • βœ” How to choose the right collection for the job
  • βœ” How iteration patterns and iterators make loops clean
  • βœ” How to sort and compare your own objects
  • βœ” A beginner‑friendly look at time complexity
  • βœ” How to build your own custom collection class
🧺
Collections: The Baskets of Programming
The Big Idea

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.

πŸŽ’ A list is a backpack (things in order). A set is a bucket of unique stickers. A map is a phonebook (name β†’ number).
πŸ“š
Lists, Sets, Maps & Queues
The Core Four
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
πŸ• List = pizza slices on a plate. Set = different pizza types on a menu. Map = menu with prices. Queue = the line at the counter.
🎯
Choosing the Right Collection
Decision

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.

🧰 You don't use a hammer to paint a wall. Each tool (collection) has a job.
πŸ”
Iteration Patterns: Walking Through Collections
Technique

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.

πŸ“– Reading a book page by page: you don't count to a specific page number each time β€” you just turn to the next page.
πŸ“Š
Sorting & Comparing Your Own Objects
Technique

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.

🏁 A race needs a rule: "fastest wins." Sorting objects needs a rule too β€” "lowest grade first" or "name alphabetically."
⏱️
Time Complexity: A Beginner's Intuition
Performance

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.

πŸ“š Finding a word in a dictionary (O(1)) is way faster than reading every page (O(n)).
πŸ› οΈ
Custom Collection Classes
Advanced

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.

🏷️ A custom lunchbox with compartments β€” normal lunchboxes are fine, but yours is designed for exactly the food you eat.
⚠️ Common trap: Picking the wrong collection can quietly slow your program to a crawl. For example, using a list to check "is this student already here?" forces a full scan every time. A set makes that check instant. If you notice slowness, ask: "Am I using the right collection?"

πŸ› οΈ Hands-on activities (no boring stuff!)

1

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

2

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.

3

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.

4

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.

5

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.

6

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.

πŸ† Get Certified

πŸ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (₦2,000 or 20,000 coins), and get a verified certificate with your name on it.

πŸŽ“ Sign Up to Get Certified
πŸ› οΈ Practice Tools
Hands-on simulators & labs - subscription required.
β†’
🎯 Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
β†’