Scala

Scala Maps

Scala is a modern programming language that resembles Java quite a lot. It is very convenient to comprehend and useful programming language. It is a mesh of different programming concepts and in this guide, we will be talking about one of the very important concepts of this language i.e. Scala maps.

What are the Maps in the Scala Programming Language in Ubuntu 20.04?

A map in Scala is a data structure that is used to store the data in the form of key-value pairs. It means that by referring to a key, you can easily extract the corresponding value and vice versa. However, to learn how you can create and use the maps in Scala, you must have to go through the two Scala scripts that we have created for you.

Pre-Requisites of Running a Scala Program in Ubuntu 20.04

For running a Scala program on any operating system in general and Ubuntu 20.04 in particular, you must have the Scala compiler installed on your system.

How to Compile and Execute a Scala Program in Ubuntu 20.04?

It is very easy to compile and execute a program written in Scala in Ubuntu 20.04 system. Here, we are going to elaborate the generic commands for doing that. However, before proceeding with these commands, we would like to tell you that for compiling a Scala program, you need to use the name of the script file whereas for executing it, you need to use the name of your class that you have created within your program.

The generic command for compiling a Scala program is as follows:

$ scalac filename.Scala

Here, “filename.Scala” represents the name of the Scala script file that you want to compile whereas the “scalac” keyword is used for compiling a Scala program.

Then, the generic command for executing a Scala program is mentioned below:

$ scala classname

Here, “classname” refers to the name of the class inside which you have written your whole Scala program whereas the “scala” keyword is used for executing a Scala program.

Now, you can go through the following examples to understand the working of maps in Scala in Ubuntu 20.04.

Examples of Scala Maps in Ubuntu 20.04

The two examples discussed below will demonstrate to you the two basic scenarios in which you can conveniently use the maps in Scala for achieving the desired functionality.

Example # 1

In this example, we simply wanted to share with you a program that depicts the method of creating a map in Scala that represents the relationship between different key-value pairs. The Scala script that we wrote in this regard is as follows:

In this Scala script, we have first created a class named “TrafficLights”. A class in Scala is always created by using the “object” keyword. Then, inside this class, we have defined our “main()” function with default arguments. Within this function, we have created a Scala map with the title “trafficSignals” and have assigned to it three key-value pairs. Then, we wanted to print all the keys by referring to them with the “trafficSignals.keys” notation within the “println” statement.

Similarly, we also wanted to print all the values by referring to them with the “trafficSignals.values” notation within the “println” statement. Additionally, we also wanted to check whether our newly created Scala map is empty or not (which in this case is definitely not). For that, we have used the “isEmpty” function of Scala within the “println” statement.

Once we had finished writing our Scala script, we compiled it with the following command:

$ scalac Map.Scala

Here, “Map.Scala” represents the name of our Scala script file.

Upon a successful compilation of our Scala script, we executed it with the command given below:

$ scala TrafficLights

Here, “TrafficLights” represents the name of the class that we had created in our Scala program.

When our Scala program was executed, the following output was revealed on the terminal. It shows all the keys and values of our map one by one. Moreover, it also represents that our map was not empty since the output of the “isEmpty” Boolean function is “false” in our output.

Example # 2

This example is in fact, a continuation of our first example i.e., it contains all the functionality that was there in our first example, however, apart from that, we also wanted to provide some additional functionality. We wanted to use this Scala script to check if a particular key exists within the map or not. For that, the Scala script that we wrote is as follows:

The part of this Scala script before the “if-else” statements has already been explained in depth in the first example of this article. Therefore, we will directly proceed to the explanation of the “if-else” block of our Scala script. Within the “if” statement, we have used the “contains” function to check if a given key i.e., “purple” in this case exists within our map or not. If this function will return “true”, a success message will be printed on the terminal whereas if the “else” part of this block will be executed i.e. the specified key will not exist in our map, then a failure message will be printed on the terminal.

We have already taught you the procedure of compiling and executing a Scala script, therefore, we will now show you the output of this script. In this output, you can see that the provided key i.e., “purple” does not exist within our map.

Conclusion

This article demonstrated the usage of Scala maps on Ubuntu 20.04 system. Since it was an introductory article on the Scala programming language, therefore, we also shared with you the pre-requisites of running a Scala program along with the relevant compilation and execution commands. After that, we explained to you two very easy examples that made use of the maps in Scala. After understanding these illustrations, you will quickly grasp the concept of the maps in Scala in Ubuntu 20.04.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.