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




KOTLIN - INSERT IN SET


How to insert a new Item at the end of the Set?


Let us say, we have a Set that contains three names, Mohan, Kriti and Salim. And we want to insert a new name Mika at the end of the Set.


We can use the add() Function without any parameter to achieve the above.


Example :



fun main() {
    var x = mutableSetOf("Mohan", "Kriti", "Salim")
    x.add("Mika")
    println(x)
} 


Output :



  [Mohan, Kriti, Salim, Mika]

So, in the above code we have created a Set and initialised to the variable x.


var x = mutableSetOf("Mohan", "Kriti", "Salim")

Below is how the values are positioned in the Set,

java_Collections

Next, we have used the add() function to add the new name Mika at the end of the Set.


x.add("Mika")
java_Collections


And we get the below output,


[Mohan, Kriti, Salim, Mika]