Scala provides rich set of collection libraries which contains classes and traits to collect data. These collections can be mutable or immutable in nature.
All the mutable collections are present in Scala.collection.mutable package and all the immutable collections are present in Scala.collection.immutable package.
Some of the most commonly used collections are as follows:
1. Lists
Lists are used to store ordered elements. May contain duplicate elements and generally good for last-in-first-out (LIFO), stack-like access patterns.
Lists are very much similar to arrays.
Example
object Lists {
def main(args: Array[String]): Unit = {
var listOfNumbers: List[Int] = List(10, 40, 20, 80, 60, 200, 150);
println(listOfNumbers);
}
}
Try yourself here
2. Sets
Sets are used to store unique elements and they don't maintain any order. They can be mutable or immutable.
Example
object Sets {
def main(args: Array[String]): Unit = {
var mobiles = Set("iPhone", "Samsung", "OnePlus", "Nokia");
println(mobiles);
}
}
Try yourself here
3. Maps
Maps are used to store key-value pairs. You can either use , or -> operators to specify key-value pairs as shown below. Notice you need to enclose key-value pair in () when you use ,.
Example
object Maps {
def main(args: Array[String]): Unit = {
var mobiles1 = Map(("iPhone", " iPhone 11 pro"), ("Samsung", "Galaxy S20"));
var mobiles2 = Map("iPhone" -> " iPhone 11 pro", "Samsung"-> "Galaxy S20");
println(mobiles1);
println(mobiles2);
}
}
Try yourself here
4. Tuples
Tuples are used to store ordered elements of different types like you can mix interger values with string values and float values etc. These values can be of same type as well.
Example
object Tuples {
def main(args: Array[String]): Unit = {
var tup = ("iPhone", 1, "$1299", 5.6);
println(tup);
}
}