Scala

Scala Tuples

A Scala tuple is simply defined as a collection of elements. However, unlike arrays, a tuple is not homogenous in other words, it is heterogeneous which means that a tuple can hold values belonging to different data types together at the same time. In this article, we will be using the Scala tuples in different ways in Ubuntu 20.04.

How to Use the Tuples in Scala?

The following examples will demonstrate the usage of Scala tuples in Ubuntu 20.04:

Example # 1: Creating and Printing an Integer Tuple in Scala

In our first example, we will be teaching you the method of creating and printing an integer tuple in Scala in Ubuntu 20.04. For that, we have implemented the following Scala script:

For this example, we have first created a class named “Tuple”. We will be using the very same class for all of our examples in this article. Then, inside this class, we have designed a “main()” function in which we have created a value named “MyFirstTuple”. Then, we have simply assigned five different integer elements to this tuple. After that, we have a “println” statement with the help of which we wanted to print the elements of this integer tuple on the terminal.

Then, we wanted to compile this script with the command shown below:

$ scalac Tuples.Scala

After that, we executed the same script with the following command:

$ scala Tuple

The output of our Scala script i.e., all the elements of our integer tuple that we had created is shown in the image below:

Example # 2: Creating and Printing a String Tuple in Scala

This example is just another variant of our first example with the help of which we wanted to explain to you the method of creating and printing a string tuple in Scala. The following Scala script shows the procedure of doing so:

In this example, we have simply created a value named “MyFirstTuple” and have assigned to it three different string elements. Then, we have printed the elements of this tuple on the terminal by using its name with the “println” statement.

All the elements of our string tuple are shown in the image below:

Example # 3: Creating and Printing a Mixed Valued Tuple in Scala

This example is a little different from the two examples that we have discussed above. An interesting feature of the Scala tuples is that they can easily hold multiple values of different data types together unlike arrays. Therefore, in this example, we will share with you the method of creating and printing a Scala tuple having mixed values. For that, we have designed the following Scala script:

In this example, we have created a tuple in the same manner as we did above. However, this time, we have assigned values belonging to different data types to this tuple i.e., string and integer values in this case. Nevertheless, the method of printing these elements on the terminal is the same.

The elements of our mix valued tuple are shown in the image below:

Example # 4: Accessing a Specific Element of a Tuple in Scala

In this script, we will tell you how you can access any specific element of a tuple if you wish to do so. For achieving this objective, we have implemented the following Scala script:

In this example, we wanted to access the 7th element of a tuple. For that, we have used the same tuple that we have created in our third example. Then, we have just printed all the elements of this tuple on the terminal just for the record. Finally, for accessing and printing the 7th element of this tuple on the terminal, we have used the “MyFirstTuple._7” notation. You can replace “7” with any integer depending upon the exact position of the element that you want to access within the target tuple. (Note that the numbering of elements within a tuple always start from “1”)

The output of this Scala program is shown in the image below which confirms that the 7th element of our tuple has been printed correctly on the terminal:

Example # 5: Swapping the Elements of a Tuple in Scala

At times, you might want to swap the positions of the elements within a tuple. For teaching you that, we have designed the following Scala script:

In this script, we have created a Scala tuple with the title “MyFirstTuple” and have assigned to it two different elements. We want to swap the positions of these elements in a way that the first element will become the second and the second one will become first. For that, we have simply used the “swap” function of Scala with the name of our tuple.

After executing this script, the output shown below appeared on our terminal which confirms that the elements of our tuple have been swapped successfully.

Example # 6: Printing the Elements of a Tuple in Scala While Using the For Each Loop

The elements of a Scala tuple can also be printed on the terminal by using the “foreach” loop. For doing that, you can take help from the following Scala script:

In this example, we have just created an integer tuple. Then, by using the “productIterator” built-in function along with the name of our tuple and the “foreach” loop, we wanted to print the elements of our tuple on the terminal in a way that each element is printed on a separate line.

The elements of our tuple printed on the terminal with the “foreach” loop are shown in the image below:

Conclusion

With this article, we wanted to explain to you the method of using the Scala tuples in Ubuntu 20.04. We first introduced you to this data structure by explaining its functionality. Then, we progressively discussed six different examples that elaborated the usage of this data structure in the Scala programming language. By taking help from these examples, you can conveniently use this data structure for serving your programming needs.

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.