Mutable vs. Immutable Collections in the Scala Programming Language
Mutable collections are the ones in which the contents of the reference can easily change, whereas immutable collections are the ones in which the contents of the reference can never change. The Scala programming language supports both mutable and immutable collections, however, by default, the collections in Scala are immutable. If you want to make a collection mutable, then you will have to import additional libraries whereas the immutable collections can be used as it is.
Most Commonly Used Collections in the Scala Programming Language
There are four main collections that are used most commonly in the Scala programming language and we are briefly going to discuss them below:
- Scala List– It is a Scala collection that contains elements belonging to the same data type. The Scala lists are immutable.
- Scala Set– It is a Scala collection that contains pairwise different elements that belong to the same data type. The Scala sets can be mutable or immutable.
- Scala Map– It is a Scala collection that contains elements in the form of key-value pairs where the keys cannot be repeated. It means that the keys of a Scala map must be unique whereas there is no compulsion on the uniqueness of the values of a Scala map. The Scala maps can also be mutable or immutable.
- Scala Tuple– It is a Scala collection containing a fixed number of items belonging to the same or different data types. A Scala tuple is always immutable.
Using the Different Collections in the Scala Programming Language in Ubuntu 20.04
For learning the usage of the different collections in the Scala programming language, you will have to understand the following four examples. However, we will only be discussing the basics of using each of these collections over here i.e., creating a particular Scala collection and printing it on the terminal.
Example # 1: Creating and Printing a List in Scala
In this example, we will create a Scala list and print its elements on the terminal. For that, we have implemented the code shown below in the Scala programming language:
In this Scala code, we have created an object class with the name “Collections”. Then, inside the “main()” function of this code, we have created a list named “courses” and have assigned to it the names of four different courses as its elements. After creating this list, we wanted to print its elements on the terminal. We can do this simply by passing the name of this list to the “println” function.
We can compile this simple Scala script with the command given below:
Then, we can run this Scala code with the following command:
The elements of the list that we have just created are shown in the image below:
Example # 2: Creating and Printing a Set in Scala
In this example, we will be creating and printing our second Scala collection i.e., Set. For that, we have made use of the following Scala code:
In this example, we have used the same “Collections” class for enclosing our actual script. Then, within the “main()” function, we have created a set named “seasons” and have assigned to it the names of the four seasons as the elements of this set. After that, for printing the elements of this set on the terminal, we have passed the name of this set to the “println” function.
The elements of our set upon the execution of our Scala script appeared on the terminal as shown in the image below:
Example # 3: Creating and Printing a Map in Scala
Now, we want to create and print our third Scala collection on the terminal i.e., Map. For that, we have implemented the following Scala program:
In this program, we have defined a Scala map with the name “standard”. After that, we have assigned to it three different key-value pairs. In this map, the standards i.e., 1, 2, and 3 serve as the keys whereas the names i.e., John, Harry, and Frank serve as the values. Then, we wanted to print all the keys and values of this map separately on the terminal for which we have used two “println” statements.
All the keys and values of our Scala map are shown in the image below:
Example # 4: Creating and Printing a Tuple in Scala
Finally, we will create and print our fourth Scala collection i.e., Tuple in this example. For that, we have used the Scala code shown in the following image:
In this Scala code, we have defined a mixed valued tuple and have assigned to it three values belonging to different data types. The first one is an integer, the second one is a string whereas the third one is a floating point number. You can also assign values to a tuple that belong to the same data type. To print all the values of this tuple on the terminal, we have passed the name of this tuple to the “println” function.
All the values of our mixed valued tuple are shown in the image below:
This was a generalized article based on the Scala collections in Ubuntu 20.04. We wanted to explore this topic with respect to the most commonly used collections in the Scala programming language namely “list”, “set”, “map”, and “tuple”. For that, we shared with you four different examples each of which explained the usage of the said Scala collections one by one. By reading this article, you would have gotten a thorough idea of the most commonly used collections within the Scala programming language in Ubuntu 20.04.