Scala

Scala Map Foreach

As technology tends to improve daily, it also creates chaos among developers and adds pressure to learn more languages, tools, and technologies to adapt to the change. One of the not-so-old languages in Computer science is Scala which presents diverse functions. For instance, the “foreach” is said to be a loop in different object-oriented programming languages to iterate all the elements. In contrast, Scala programming is used to apply a specific function to all the elements of a map structure, i.e., some lists, etc. Hence, we are certain to discuss using the map foreach concept in Scala programming by utilizing the Ubuntu 20.04 system.

Example 01

We will begin our article with a simple example of a Scala so that this article won’t be a roller coaster ride for your brain. You need to make sure that Scala is configured on your Linux machine without any issue and generate a file containing the “scala” extension at its end via the console application. Open this Scala document in a text copyreader to quickly write and save the script. We have been starting this script with the initialization of an object “test” that will be used on execution after the compilation.

Within it, we have defined our main() execution function that contains a variable “v”. This variable contains elements in a Map function, i.e., some integer keys with their string values. Now, the variable “v” has mapped key-value pairs, and we have initialized another variable, “r”, at the 4th line of code. The mapped variable “v” has been calling the “foreach” function to iterate and print the keys and values of the mapped variable separately using the “println” function statement within it with the help of the “n” variable, i.e., used as an index number. Index 1 of first any value would be used as a key, while index 2 will be used as a value. Let’s save our code before its compilation at the shell.

On listing the contents of our Linux directory, we found our Scala file in it. So, we have been compiling this Scala file using the scalac compiler of Scala and got the object file after the compilation. Now, we have been using the Scala instruction to run the object file using the object name used in the code, i.e., “test”. The output shows the mapped values as key-value pairs on our console screen. All the key-value mapped pairs have been displayed in a random order, i.e., in which these are initialized in the code.

Within the above code, we have used the foreach function on the mapped values that are fully unique and have no duplications in any of the keys or values. Then what if we use the duplicate values in a map? What will be the outcome of using the same “foreach” method on the map? Will it throw an exception or just skip the duplicate value? These will be discussed in the below-shown Scala code.

So, we have updated the previous code to make it different and check for duplicate values. Starting from the object “test”, the same main() function has been defined. It contains the same name variable “var”, using the Map to get the values. It contains a total of 5 values while 2 of its values are the same. Key 3 and key 4 contain the same value, “Paul”, while there are two same keys, i.e., key number 3. The very same variable “r” has been used to apply the foreach function on the map “v” to separate its keys and values and display them on the console via the println() function and variable “n”. Let’s save and run the code to see what occurs to replica values.

On compilation and execution of the Scala code, we have 4 key-value pairs displayed on the console. While the value that occurred twice has been displayed only once using the key “4”. This means the foreach function enables you to use only 1 value from the duplicate.

Example 02

Let’s get started with the last example of this guide to elaborate on the use of the foreach() function on Map differently. In this example, we will show you how to separate keys and values from the Map data and use value names at the place of keys. So, we started this example code with the same test object containing the main() function definition. The main() function has been initializing a map “m” with a total of 5 key-value pairs.

All the values in a map are unique and distinct. The variable “k” has been initialized with the use of the foreach() function on the keys of a map “m” to separate keys and print their respective values. At the very next line, we have initialized another variable, “v”, that has been applying the “foreach” function on the values of a map “m” to separate values from it and display them on the console solely using the println() function statement. This way, the values would also take the place of keys on the console. Now that the program is sufficient, we will be saving it first.

The output shows the Map data key-value pairs at the first 5 lines in the random order with the keyword “Some”. After this, all the values from map “m” are displayed with the same name keys at the following 5 lines:

Conclusion

Within this Scala guide, we have discussed using the foreach() function on the map data. It successfully returns the map data after applying different operations to the map data. We have tried it for key-value pairs to combine them, remove the duplicates, and separate keys from values at the console screen. Using the foreach function on the map is not so difficult but not so easy as it is a little tricky. Practice is key.

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.