Scala Lesson 12 – Sets and Maps | Dataplexa

Sets and Maps in Scala

In this lesson, you will learn about Sets and Maps in Scala. These collections are used when you need unique values or key–value relationships.

Sets and Maps are widely used in real-world Scala applications such as configuration handling, data validation, fast lookups, and data processing pipelines.


What Is a Set?

A Set is a collection that stores unique elements only. If duplicate values are added, Scala automatically removes them.

By default, Scala provides immutable sets, which cannot be modified after creation.


Creating a Set

val numbers = Set(1, 2, 3, 3, 4)
println(numbers)

Even though 3 appears twice, the set contains only one instance.


Checking for an Element in a Set

Sets are optimized for fast membership checks.

println(numbers.contains(2))
println(numbers.contains(10))

This returns true if the element exists, otherwise false.


Adding Elements to a Set

Since sets are immutable, adding an element creates a new set.

val newNumbers = numbers + 5
println(newNumbers)

The original set remains unchanged.


Removing Elements from a Set

val updatedSet = newNumbers - 3
println(updatedSet)

This removes the specified element and returns a new set.


Iterating Over a Set

You can loop through a set just like other collections.

for (n <- numbers) {
  println(n)
}

The order of elements in a set is not guaranteed.


What Is a Map?

A Map stores data as key–value pairs. Each key is unique and maps to a single value.

Maps are commonly used for fast lookups, configurations, and dictionaries.


Creating a Map

val ages = Map(
  "Alice" -> 25,
  "Bob" -> 30,
  "Charlie" -> 28
)

println(ages)

Here, names are keys and ages are values.


Accessing Values in a Map

You can access values using their keys.

println(ages("Alice"))

This returns the value associated with the given key.


Safe Access Using get()

To avoid errors when a key does not exist, use get().

println(ages.get("Bob"))
println(ages.get("David"))

The result is an Option, which may contain a value or be empty.


Adding Elements to a Map

Adding a new key-value pair returns a new map.

val updatedAges = ages + ("David" -> 35)
println(updatedAges)

Removing Elements from a Map

val reducedMap = updatedAges - "Charlie"
println(reducedMap)

This removes the entry with the specified key.


Iterating Over a Map

You can loop through key-value pairs using pattern matching.

for ((name, age) <- ages) {
  println(name + " is " + age + " years old")
}

When to Use Sets and Maps

  • Use Sets when uniqueness matters
  • Use Maps when data has a key–value relationship

Both collections are essential tools in Scala programming.


📝 Practice Exercises


Exercise 1

Create a set of five numbers and check if a value exists.

Exercise 2

Create a map of country names and their capitals.

Exercise 3

Add a new entry to a map and remove an existing one.


✅ Practice Answers


Answer 1

val data = Set(2, 4, 6, 8, 10)
println(data.contains(6))

Answer 2

val capitals = Map(
  "India" -> "New Delhi",
  "France" -> "Paris"
)
println(capitals)

Answer 3

val updated = capitals + ("Germany" -> "Berlin") - "France"
println(updated)

What’s Next?

In the next lesson, you will learn about Immutability and why it is a core concept in Scala and functional programming.