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




KOTLIN - REVERSE A SET


How to reverse a Set?


Reversal of a Set can be done using reversed() method. It is independent of the alphabets. And is not a sort. It is just a reversal.


Example :



fun main() {
    var x = mutableSetOf("Mohan", "Kriti", "Salim")
    var y = x.reversed()
    println("The Set in reversed order is "+y)
}


Output :



  The Set in reversed order is [Salim, Kriti, Mohan]

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

Then we have used the reversed() Function to reverse the elements of the Set x.


var y = x.reversed()

And the Set x gets sorted in reverse order with Salim as the first value, Mohan second and Kriti as the third.

java_Collections

And we get the below output.


The Set in reversed order is [Salim, Kriti, Mohan]