What are Sets in the Scala?
As we have already mentioned that a set in Scala does not contain any duplicates; therefore, now we will extend our explanation further. A Scala set contains similar elements; however, no two elements of a set can be the same. Scala sets can either be mutable or immutable; however, the immutable sets can be used without importing any external libraries. If you wish to use the mutable sets in Scala, you will have to import the “scala.collection.mutable.Set” library.
How to use the Sets in the Scala Programming Language?
Now, for learning how to use the sets in the Scala programming language, you will have to understand the following examples that we have implemented for you.
Example # 1: Printing the Elements of a Scala Set:
In this example, we will learn the method of declaring a set in Scala, populating it with some values, and then printing them on the terminal. The following Scala script will help you in doing so:
We can declare and use the immutable sets in Scala without having the need of importing any libraries. First, we have declared a class named “Sets”. Then, within this class, we have declared a set named “colours” and assigned the names of four different colors to it. Then, we also wanted to print these elements on the terminal. For that, we have simply used the name of our declared set within the “println” statement. In this way, you will be able to access all the elements of the specified set at once.
After designing this Scala script, we compiled it by using the command shown below:
Then, we have used the name of our driver class with the following execution command for executing our Scala script:
The image listed below represents the output of this Scala script, i.e. all the elements of our declared set are printed on the terminal.
Example # 2: Checking if a Particular Element Exists within a Scala Set or not:
After declaring and initializing a set, you might also want to find out if a particular element exists within your set or not. For that, you need to compare that element with all the set elements. If a match is found, your function will return “true”; otherwise, it will return “false”. You can use the following Scala script for implementing this functionality:
In this example, we have declared a set and assigned the names of four colors to it. Then, we have printed all the elements of this set on the terminal. Finally, to check if a particular element exists within our declared set or not, we have used the “colours.contains” notation. Here, “colours” refers to the name of our set, whereas “contains()” is the function that will compare the element passed to it (which in this case is “green”) with all the elements of the set to check whether this element is a part of that set or not. This is also a Boolean function which means that if the specified element exists within our set, then it will return “true”; otherwise, it will return “false”.
You can view the output of this Scala script in the image shown below. It implies that the specified color, i.e. “green” does not exist in our set since the “contains()” function returned “false” in this case.
Example # 3: Finding the Maximum and Minimum Element of a Scala Set:
In this example, we will be sharing with you the method of finding the maximum and minimum elements from a given set. For that, you will have to write a Scala script similar to the one shown in the following image:
We have declared a set named “numbers” within this Scala script and have initialized it with five random integer values. Then, we have simply used the name of this set to print its elements on the terminal. After that, for finding the maximum element from all the elements of this set, we have used the “max” function of the Scala programming language along with the name of our set and have printed its output on the terminal. Similarly, for finding the minimum element from all the elements of this set, we have used the “min” function of the Scala programming language paired with the name of our set and have printed its output on the terminal as well. The “max” and “min” functions are the pre-defined functions of the Scala programming language. Therefore, you can use them straightaway without defining their functionality.
The image shown below represents the output of this Scala script. It first printed the elements of our set on the terminal. After that, it successfully displayed the maximum and minimum element of this set on the terminal, which is “22” and “3” respectively.
This article aimed to teach you the concept of the sets in the Scala programming language. We first discussed some important properties of this data structure, after which we explained to you three different examples that used the Scala sets in different ways. You can bring in more variation to these examples depending upon the functionality that you want to achieve. However, our goal was just to give you an overview of Scala’s work, which is pretty much achieved within this article.