Scala

Scala GroupBy

Grouping is the key to success when it comes to teamwork at a certain level. Every individual in their life must have been a part of some group, i.e., a group of friends, a team, a group of classmates, etc. The concept of grouping has always been very popular among developers. Collection data structures in Scala have GroupBy as a component. The items of collections are grouped using this technique, as the term may imply. Both mutable and immutable collections in Scala are compatible with this GroupBy. A once-assigned entity is said to be immutable if they are unable to modify its value by itself.

In contrast, a constantly changing entity is said to be mutable. Furthermore, this GroupBy turns the Collection into a Map, allowing us to get it for several important functions. We shall receive a map in response that contains the key-value pair. This article explains the use of the Groupby clause in the Scala language.

Example 01

To understand the GroupBy function properly, we need to take a good look at Scala program examples. Thus, we will be starting with the most basic one in which we will be grouping a simple list. The Scala file must be created with the “.scala” extension, and it has been launched within the simple text editor. The Scala must be started with some object that would be used to execute the object code at the end. In each programming language, the main execution always starts from the driver function of a code. So in our case, we have created an object named “test” and added the main() function to drive our Scala code.

The main function execution starts with creating a string type list variable named “l” and the initialization of 5 string values. At the very next line, the main function executes the println() function to display that it is going to display a list before the use of the Groupby clause or function. Then, it tried the println() function with the “l” list to display a list. The next consecutive line number 6, another variable “n” was initiated to save the resultant list that has been modified by the use of the GroupBy() function on each of its string elements “x”. The next consecutive println() function statements are utilized again to show that we will display the List after using the GroupBy clause of function on each of the list elements and display the list “n”.

Now, our code is completed and ready for use. So, save it first and launch the console.

On opening the shell console, we listed the files and folders of our Linux current directory and got the test.scala listed in it. The scalac compiler query for Scala was utilized to compile the previous code, and the object code file was created in return. That object file was executed using the object name “test” in the Scala query and displayed the list before and after using the GroupBy clause. You can see that the GroupBy clause can randomly set the elements.

The previous example code was all about using the GroupBy clause or function on the list of strings. What would its output be when we use a list of numbers? Let’s look at the same example code with a slight change. We have replaced the list “l” with an integer type list of 6 numbers. The rest of the Scala code has remained the same as the following image. Let’s quickly save this Scala updated code and go back to the shell for compilation.

After compiling the updated Scala code, we executed it using its object name, i.e., “test”, and have the following output. You can see that the GroupBy clause has randomly grouped all the integer elements of a list at the console screen shown below:

Example 02

Have you ever experienced how a GroupBy clause or function reacts to a duplicate value on the application? Let’s check it now. We have updated the source code file of Scala and added the string list “l” in the main() function that contains some repeated values. Then a list was used for printing. After this, we applied the GroupBy() function to the “Linux” string element of this list via the contains() function. It will group only the duplicates of element “Linux” and place the rest on a Map. The following two println() statements are utilized for displaying:

We have again compiled and executed the Scala code file and its object file. On running, we have the list as it is displayed before the use of the GroupBy function. After that, we have the list of elements other than the element “Linux” displayed as a Map, and “Linux” elements are grouped in a separate list as presented.

You can also use the GroupBy function clause to display a single character from each element of the list using their index number in a charAt() function. So we have been using the very same string list “l” in the code, and the println() function is castoff for displaying it at the console. Then, the charAt() function is utilized in the GroupBy function to display 2nd character of each item.

After saving the code file, we have compiled and executed it using shown instructions on the console, i.e., scalac compiler and Scala executor. The list has been grouped by the display of the 2nd character of each element at the end.

Conclusion

We have put an immense amount of hard work into writing this article to provide the best guide to our learners. Starting from the simple introduction, we have explained how a GroupBy function can be castoff in Scala language to create a collection of data. With that, we have explained different examples of Scala utilizing the Groupby function and some sub-functions to perform different operations and set the data into a collection.

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.