This article explores different ways to convert a set to a list using Kotlin.

1. Using toList() function

The standard way to convert a set to a list is using the toList() function.

Download Code

 
It returns an immutable list instance. To get a mutable list, you can use the toMutableList() function.

Download Code

2. Using Copy Constructor

We can use a copy constructor, which can take another collection object to construct a new list containing all elements of the specified set.

Download Code

3. Using Spread Operator

You can also use the listOf() function using the Spread operator by prefixing its array implementation with *.

Download Code

4. Using for loop

Another solution is to create an empty list and push every element of the specified Set into the list.

Download Code

5. Using addAll() function

Instead of using the for-loop, you can use the addAll() to add all elements of the specified Set to the list.

Download Code

That’s all about converting a set to a list in Kotlin.