The Scala programming language has many useful built-in functions that you can use for performing complex programming problems. Additionally, these functions also help you carry out routine tasks very efficiently. This guide will teach you about the Collect method of the Scala programming language and its usage on the Ubuntu 20.04 system.

What is the Purpose of using the Collect Method in Scala?

At times, you create different types of collections in Scala. A collection is defined as a container that can hold data. An array can only hold elements having the same data type, whereas a collection can also hold elements belonging to different data types together. A Collect method is used whenever you want to extract a particular type of element from a collection that contains the elements belonging to different data types. For example, a Scala collection might contain the integer, string, and float type elements, and you only want the String elements out of these. This is where the Collect method will come into play, as it will help you extract the String elements only from your collection. You will learn more about this method by going through the next section of this article.

Examples of using the Collect Method of Scala in Ubuntu 20.04:

To learn how you can use the Collect method in Scala in Ubuntu 20.04, you will have to look at the examples explained below:

Example # 1: Extracting the String Values from a Given Collection:

In this example, we will try to create a sample collection that will contain values belonging to different data types. After that, we will try to extract only the String values from this collection. For doing this, we have used the following Scala script:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Scala-Collect-Method-1.png" data-lazy- height="361" src="data:image/svg xml,” width=”647″>

We first created a class named “Collect” using the “object” keyword. Then, within our defined “main()” function, we wanted to notify the user that we will extract or collect only the String values with the help of this Scala script. For that, we have used the “println” statement. After that, we have created a sequence collection that can contain the elements belonging to any data type. You can see that we have populated this sequence collection with some integer and some string values. Then, we have created a value with the name “output” and have assigned the output of the Collect method, which is applied over our declared collection. This method is implemented to collect only the String values from the given collection. Finally, to find out if the String values have been collected successfully or not, we wanted to print the value of the “output” variable on the terminal by using another “println” statement.

We have used the command shown below to compile this Scala script:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Scala-Collect-Method-2.png" data-lazy- height="24" src="data:image/svg xml,” width=”425″>

Then, we have used the command that follows for executing our Scala script:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Scala-Collect-Method-3.png" data-lazy- height="22" src="data:image/svg xml,” width=”365″>

The output of this Scala script is depicted through the image shown below. You can easily verify that our Collect method has successfully extracted the String values from our sequence collection.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Scala-Collect-Method-4.png" data-lazy- height="73" src="data:image/svg xml,” width=”601″>

Example # 2: Extracting the Integer Values from a Given Collection:

This example is a modified version of our first example, as in this one, we will try to extract only the integer values from the given collection. The Scala script that we have used for this purpose is shown in the following image:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Scala-Collect-Method-5.png" data-lazy- height="362" src="data:image/svg xml,” width=”647″>

Again, in this example script, we have declared a Scala class named “Collect” with the “object” keyword. Then, we have notified the user about the functionality of this particular script by using the “println” statement within our “main()” function. After that, we have created a collection with random integer and string values. Finally, we have created an “output” value to hold the result of our “Collect” method that will extract all the integer values from the given collection, and then, we have used the “println” statement for printing these results on the terminal.

After compiling and executing this Scala script, the result we managed to get is shown in the image below. You can verify that our “Collect” method could print the integer values successfully on the terminal.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Scala-Collect-Method-6.png" data-lazy- height="79" src="data:image/svg xml,” width=”614″>

Example # 3: Extracting the Double Values from a Given Collection:

This example is yet another modified version of our first two examples. We will extract only the double values from a given collection in this instance. The Scala script that we have used for this purpose is as follows:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Scala-Collect-Method-7.png" data-lazy- height="364" src="data:image/svg xml,” width=”648″>

Within our “Collect” class, we have created a “main()” function for writing the main code of our Scala script. Again, for notifying the user, we have printed a message on the terminal using the “println” statement regarding the functionality of our Scala script. Then, we have created a sample collection that consists of a random string, integer, and double values. After that, we have created an “output” value to hold the result of our “Collect” method that will extract only the double values from the given collection. Finally, we wanted to print the result of the “output” value on the terminal to verify if our “Collect” method has successfully managed to collect the double values or not from our given collection.

The result of our Scala script is, shown in the image below, verifies that our Collect method has performed its job correctly, i.e., it has extracted the double values successfully from our collection.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Scala-Collect-Method-8.png" data-lazy- height="78" src="data:image/svg xml,” width=”613″>

Conclusion:

This discussion was primarily based on developing an understanding regarding the usage of the Collect method of the Scala programming language. We first introduced you to the basic purpose of this method, after which we shared three different example scripts with you to elaborate on this purpose in a better way. Now, you will be able to use this method of Scala very effectively after going through all of these examples.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/IMG-20211027-WA0029-150×150.jpg62205788de5c6.jpg" height="112" src="data:image/svg xml,” width=”112″>

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.