Collections in Scala
In this lesson, you will learn about collections in Scala. Collections are used to store, organize, and process groups of data efficiently.
Scala provides a rich and powerful collection library that supports both immutable and mutable data structures.
What Is a Collection?
A collection is a container that holds multiple values. These values can be processed together using built-in operations.
Instead of working with single variables, collections allow you to work with groups of data in a clean and expressive way.
Why Collections Are Important
Collections allow you to:
- Store multiple values efficiently
- Perform operations like filtering and mapping
- Write concise and readable code
- Process large datasets easily
Immutable vs Mutable Collections
Scala collections are divided into two main categories:
- Immutable collections – cannot be changed after creation
- Mutable collections – can be modified
By default, Scala encourages the use of immutable collections for safer and more predictable code.
Common Collection Types
Scala provides several commonly used collection types:
- List
- Array
- Set
- Map
In this lesson, you will get an overview of these collections. Each one will be explained in detail in upcoming lessons.
Creating a List
A List is an immutable collection that stores ordered elements.
val numbers = List(1, 2, 3, 4, 5)
println(numbers)
Once created, the list cannot be modified.
Accessing List Elements
You can access elements using index-based access.
println(numbers(0))
println(numbers(2))
Scala uses zero-based indexing.
Creating an Array
An Array is a mutable collection. Its elements can be modified after creation.
val fruits = Array("Apple", "Banana", "Orange")
fruits(1) = "Mango"
println(fruits.toList)
Arrays are useful when you need mutable, index-based data.
Creating a Set
A Set stores unique elements only. Duplicate values are automatically removed.
val uniqueNumbers = Set(1, 2, 2, 3, 4)
println(uniqueNumbers)
Sets are useful when uniqueness matters.
Creating a Map
A Map stores data as key-value pairs.
val studentAges = Map(
"Alice" -> 22,
"Bob" -> 24
)
println(studentAges("Alice"))
Maps are useful for fast lookups based on keys.
Basic Collection Operations
Scala collections support many built-in operations.
Looping Through a Collection
for (n <- numbers) {
println(n)
}
Using map()
The map() method transforms each element.
val doubled = numbers.map(n => n * 2)
println(doubled)
Using filter()
The filter() method selects elements based on a condition.
val evenNumbers = numbers.filter(n => n % 2 == 0)
println(evenNumbers)
Why Scala Collections Are Powerful
Scala collections are:
- Type-safe
- Immutable by default
- Rich in built-in operations
- Optimized for functional programming
📝 Practice Exercises
Exercise 1
Create a list of five numbers and print it.
Exercise 2
Create a set with duplicate values and observe the output.
Exercise 3
Create a map that stores country names and capitals.
✅ Practice Answers
Answer 1
val nums = List(10, 20, 30, 40, 50)
println(nums)
Answer 2
val data = Set(1, 1, 2, 3, 3)
println(data)
Answer 3
val capitals = Map(
"India" -> "New Delhi",
"USA" -> "Washington"
)
println(capitals)
What’s Next?
In the next lesson, you will explore Arrays and Lists in detail, including performance differences and real-world use cases.