Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




KOTLIN - SET


A 'Set' is a Collection that can also hold multiple values. And a 'Set' is unordered and doesn't allow duplicate values.


The declaration of a 'Set' 'Data Type' is super easy. You can place multiple values inside the method 'setOf()' or 'mutableSetOf()' and Kotlin will understand that it is a 'Set'.


Creating a read only 'Set' with values of different Data Types using setOf()


Using the 'setOf()' method, we can create an immutable set(i.e. A read only Set).


Example :



fun main() {
    var x = setOf(5, "John", "Kotlin")
    println(x)
}


Output :



 [5, John, Kotlin]

So, in the above code we have created a 'Set' using the 'setOf()' method.


And put an Integer type value (i.e. 5) and two String type value (i.e. 'John' and 'Kotlin')


var x = setOf(5, "John", "Kotlin")

And initialised to the variable 'x'.


java_Collections

So, we can see that two different data types are assigned to a 'Set'.


In the next line we have printed the 'Set' using the print statement.


println(x)

Now, if we see the output,


Output :



 [5, 'John', 'Kotlin']

The values are enclosed inside square brackets '[]'. This means that the values are inside a Set.


Set doesn't accept duplicates


Example :



fun main() {
    var x = setOf(5, "John", "Kotlin", "John")
    println(x)
}


Output :



 [5, John, Kotlin]

So, in the above code we have created a 'Set' using the values, 5, "John", "Kotlin" and "John".


var x = setOf(5, "John", "Kotlin", "John")

Now, if we see the output,


[5, John, Kotlin]

You can see only three values are printed.


This is because in the above Set, the name 'John' is present twice.


And since a Set doesn't accept duplicates, the name 'John' is not inserted to the set twice.


And we see the above output with 'John' present only once.


So, in the above code we have created a Set with 2 different Datatype(i.e. String and Integer).


Now, what if we want to restrict the Set to accept only a String or an Integer or a Float.


Let's see in the below example.


Restricting the Set to accept elements of one Datatype


Example :



fun main() {
    var x: Set<String> = setOf(5, "John", "Kotlin")
    println(x)
} 


Output :



 Type inference failed. Expected type mismatch: inferred type is Set<Any> but Set<String> was expected

So, what we have done is, created a Set with three values, '5', 'John' and 'Kotlin'.


var x: Set<String> = setOf(5, "John", "Kotlin")

And at the time of initialisation, we have ended up with an error.


Type inference failed. Expected type mismatch: inferred type is Set<Any> but Set<String> was expected

This is because we have restricted the Set to accept only values of String datatype.


java_Collections

And the values we are trying to use contains an integer value i.e. '5'.


java_Collections

Now, let us try inserting all String values.


Example :



fun main() {
    var x: Set<String> = setOf("Tom", "John", "Kotlin")
    println(x)
}


Output :



 [Tom, John, Kotlin]

Now, if you look at the output. We got the correct entries printed.


Creating a mutable 'Set' using mutableSetOf() method


Using the 'mutableSetOf()' method, we can create a mutable set(i.e. The Set values can be changed/replaced).


Example :



fun main() {
    var x = mutableSetOf(5, "John", "Kotlin")
    println(x)
}


Output :



 [5, 'John', 'Kotlin']

So, the values are enclosed inside square brackets '[]'. This means that the values are inside a Set.


Now, what if we don't want the output enclosed in square brackets '[]'.


Let us check that in the next tutorial.