Lists are ordered, immutable collections of elements.
val numbers = List(1, 2, 3, 4, 5)
val names = List("Alice", "Bob", "Charlie")
Arrays are ordered, mutable collections of elements.
val numbers = Array(1, 2, 3, 4, 5)
numbers(0) = 10 // Modifying an element
Sets are unordered collections of unique elements.
val uniqueNumbers = Set(1, 2, 3, 4, 5, 1, 2) // Set(5, 1, 2, 3, 4)
Maps are collections of key-value pairs.
val ages = Map("Alice" -> 30, "Bob" -> 25, "Charlie" -> 35)
Scala provides a rich set of operations for working with collections.
The map
operation applies a function to each element in a collection and returns a new collection with the results.
val numbers = List(1, 2, 3)
val squaredNumbers = numbers.map(x => x * x) // List(1, 4, 9)
The filter
operation selects elements from a collection that satisfy a given condition.
val numbers = List(1, 2, 3, 4, 5)
val evenNumbers = numbers.filter(x => x % 2 == 0) // List(2, 4)
The fold
operation combines the elements of a collection into a single value.
val numbers = List(1, 2, 3, 4, 5)
val sum = numbers.fold(0)((x, y) => x + y) // 15
The reduce
operation is similar to fold
, but it does not require an initial value.
val numbers = List(1, 2, 3, 4, 5)
val product = numbers.reduce((x, y) => x * y) // 120
Scala distinguishes between immutable and mutable collections. Immutable collections cannot be modified after they are created, while mutable collections can.
Sequences are ordered