What Are the Types of Sets in Kotlin?
Immutable Set:
The set interface is used to create immutable sets. The setOf() and setOf<T>() functions in Kotlin build immutable sets. The setOf() function contains any type of object, such as Integers, Strings, and Floats. To make a set of a certain type, use the setOf<T>() function.
Mutable Set:
The MutableSet interface is used to generate mutable sets. The mutableSetOf() and mutableSetOfT>() functions are used to build mutable sets. The mutableSetOf() function creates data types, whereas the mutableSetOfT>() creates a set of a specific type.
How to Use the Set Interface in Kotlin:
To understand the use of sets in Kotlin language, we have some examples provided below:
Example # 1: Program of Using setOf() Function in Kotlin:
We have a basic example of using the setOf() function in the code implementation. Let’s execute the setOf function code below:
In the previous code, we have defined the main function, and we have the code implementation in the main function. We have created variables as “val” keywords and assigned them a name as “Integers”, “char”, and “str”. The variable “Integer” is initialized with the set of integers using the setOf function. The variable “char” is initialized with the set of characters, and the variable “str” is declared with the string values using the setOf function. Then, we have a for-loop function that traverses through the sets of integers, characters, and strings. We have used the “in” keyword in for loop, which will iterate over each item of integers, characters, and strings. Then, these iterable items will be stored in a variable “item”. The print function is passed a variable item as a string interpolation and will return the iterable items.
The output of using the setOf() function is shown below:
Example # 2: Program of a Set Using first(), last(), indexOf(), and Last Index Functions in Kotlin:
We have provided several functions in the set interface:
The first() is used to acquire the first element, and the last() functions can acquire the last element of a set.
The indexOf() method returns the value’s position. Kotlin returns -1 if the specified value does not exist. IndexOf() returns the first occurrence’s position.
The last (or only) occurrence (or -1 if the provided element does not exist in the set) of the given element in the set is returned.
In the example code, we have defined the main function. In the main function, we have declared a variable as “val” and assigned it the name “Colors”. The variable “Colors” is using the setOf function in which we have declared the set of strings as colors names. Then, in the println function, we have Colors.first() function called, which will print the first element present in the set of the variable “Colors”. We have used Colors.last() to print the last element specified in the set. Here, we have a third println statement wherein we have used the Colors.elementAt() function, which has the value “1”. This means it will return the elements on the first index of the set. We have used the Colors.indexOf() function and passed an element from the set as “pink”, which will return the index value of this element. We have used the lastIndexOf() function to return the last index from the set of “Colors”.
Example # 3: Program of Set Using contains() and containsAll() Functions in Kotlin:
We have used contains() and containAll() functions in the example code. These methods are used to determine whether or not the element of the specified set is present.
We have created the main function. First, we have created a variable as “val” in the main function and assigned it the name “Animals”. The variable “Animals” initialized the set of string values and integer values stored in the setOf() functions. Then, we have another variable defined as “Ani_Name” containing a string value. We have a println function here, which is called a contains() function. The contains() function checks whether the string value of variable “Ani_Num” is the element of the specified set. We have also created a variable as “Ani_Num”, which contains the integer value. The presence of an integer value of variable “Ani_Num” is also checked by the contains() function in the specified set and returns a Boolean value. In the end, we have the containsAll() function, which has a new set given. It will check the element of the set passed with the element of the “Animal” set and return a Boolean value.
The Boolean values output is displayed in the image below:
Example # 4: Program of the Set Using IsEmpty() Function in Kotlin:
The IsEmpty() function will return true if the set does not contain any value, or else a false Boolean value is returned.
In the previous code’s main function, we have a variable as “val”. The variable is defined as “str”, in which an empty set of strings is created. We have also created a variable with the name “integers”, which creates the empty set of integers. Then, we have checked whether the specified sets are empty or not by using the IsEmpty() function. It is used as an expression of interpolation in the println function. Here, we have a second println function in which we have checked whether both sets are equal or not by using equal to operator. The sets will be printed as we have passed them in the Kotlin println function.
The Boolean values and empty set are returned as an output, as shown below:
Example # 5: Program of Set Using add(), remove(), and clear() Functions in Kotlin:
We have used add(), remove(), and clear() functions in the mutable sets. These functions support mutable sets in Kotlin.
The add() is used for adding the elements in the set. If the item has been added to the set, it returns true, or else false is returned.
The remove() function will remove the particular element from the set.
The clear() function will remove all the set elements.
In the previous code, we have created a variable as “val” in the main function. The variable is assigned the name “Books” and initialized with the set of books names by using the mutableSetOf() function. The mutableSetOf() function is declared with the string type property. Then, we have a println function in which we have passed a variable name and used add() with the value assigned. We have also called remove() with the value assigned, which we want to remove from the set. In the end, we have a clear() function that will clear the Set of Books, and the empty set is returned.
The output is shown on the following terminal screen:
Conclusion:
In this article, we thoroughly discussed the concept of the set interface in the Kotlin language. We have also demonstrated the use of immutable sets and mutable sets with their example code implementations. We have added five unique illustrations related to Kotlin set for our novice users and experienced ones. You will have good practice after reading this article. Check the other Linux Hint articles for more tips and tutorials.