Skip to Content
Course content

57: The Collections Hierarchy: Iterable, Seq, Set, Map

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Think of the Scala collections hierarchy like a professional kitchen's storage system. At the very top, you have the concept of a "Container." Whether it's a bowl, a spice rack, or a labeled bin, the only thing they all have in common is that they hold stuff and you can go through the items one by one. That's your Iterable.

Now, as you get more specific, you start asking questions about how you need to access those items. If you need a prep line where ingredients are laid out in a specific order (and you might have three different bowls of chopped onions), you're looking at a Seq. If you have a rack of unique spices where you don't care about the order, but you absolutely cannot have two jars of the same cumin, that's a Set. And if you have a set of bins with labels on the front—like "Dessert Toppings" pointing to a bowl of sprinkles—that's a Map.

The Broad Umbrella of Iterable

In Scala, Iterable is the grandparent of almost every collection you'll touch. If a method asks for an Iterable, it's basically saying, "I don't care how you store this or if it's ordered; I just want to be able to loop over it."

I've found that beginners often try to be too specific with their types too early. If you're writing a function that just needs to print every item in a list, don't type it as List[String]; type it as Iterable[String]. It makes your code much more flexible because you can pass in a Set or a Vector later without changing a single line of logic.

When Order and Position Matter: Seq

A Seq (short for Sequence) is where we care about the index. If you need to say, "Give me the third item in this collection," you're in Seq territory. List and Vector are the two heavy hitters here. While they both inherit from Seq, they behave differently under the hood—but for most of your daily work, the Seq interface is what you'll rely on.

val guestList: Seq[String] = Seq("Alice", "Bob", "Charlie", "Alice")
// Alice is here twice, and that's fine. 
// We know Bob is at index 1.
println(guestList(1)) // Bob

Enforcing Uniqueness with Set

Sometimes, duplicates aren't just annoying—they're bugs. That's where Set comes in. A Set is essentially a collection that guarantees every element is unique. If you try to add "Alice" to a Set that already contains her, Scala just ignores the request.

I use Set constantly when I'm dealing with IDs or usernames. It's also incredibly fast for checking membership. Asking a Set "Do you contain this item?" is significantly faster than asking a Seq, which has to scan through the items one by one.

val uniqueUserIds: Set[Int] = Set(101, 102, 103, 101) 
// The second 101 is gone.
println(uniqueUserIds.contains(102)) // true

The Key-Value Relationship: Map

Finally, we have the Map. While Seq and Set are collections of single items, a Map is a collection of pairs. You have a key (which must be unique) and a value (which can be anything).

Think of this as your lookup table. Instead of searching through a list to find a user's email by their ID, you map the ID directly to the email.

val userEmails: Map[Int, String] = Map(
  101 -> "alice@example.com",
  102 -> "bob@example.com"
)

println(userEmails(101)) // alice@example.com

One quick tip: be careful with the Map(key) syntax. If the key doesn't exist, Scala will throw an exception. In a real production environment, I always use userEmails.get(101), which returns an Option, forcing me to handle the case where the user isn't found.




📋 Practical Task

Build a Conference Attendee Manager

You need to create a small system to manage attendees for a tech conference. You'll need to use the three main collection types we discussed to handle different requirements.

Requirements:

  • Create a Set of emails to ensure no one registers twice with the same email address.
  • Create a Seq (like a List) of speakerOrder to keep track of who speaks first, second, and third.
  • Create a Map called attendeeRooms where the key is the attendee's name (String) and the value is their assigned room number (Int).

Your goal: Write a small program that initializes these three collections, adds at least three entries to each, and then prints:

  1. The total number of unique emails.
  2. The name of the first speaker in the sequence.
  3. The room number for a specific attendee using the .get() method.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.