1. Overview

In this quick tutorial, we’ll explore how to convert between List and Set in Kotlin.

2. Introduction to the Problem

The problem looks pretty straightforward. We know both List and Set are subtypes of the Collection interface. Further, the Iterable interface is the supertype of the Collection interface.

The standard Kotlin library has added a set of convenient extension functions to the Iterable interface so that we can convert between List and Set easily, such as toSet(), toHashSet(), toList(), and so on

In this tutorial, we’ll address how to convert between List and Set in Kotlin. Also, we’ll discuss the differences between those extension functions.

By the way, for simplicity, we’ll use unit tests to verify the result.

3. Converting a List Into a Set

The most straightforward way to convert a List to a Set could be using the constructors of the concrete Set classes, for example:

val inputList = listOf("one", "two", "three", "four", "one", "three", "five")
val expectedSet = setOf("one", "two", "three", "four", "five")
assertThat(HashSet(inputList)).isInstanceOf(HashSet::class.java).isEqualTo(expectedSet)

The above code converts a List into a HashSet using the HashSet constructor. Similarly, we can also convert a List into a TreeSet or LinkedHashSet by using their constructors depending on the requirement.

As a Set only holds unique elements, the duplicated elements in a List won’t be added to the Set.

It’s worth mentioning that the Sets we get by calling the constructors are mutable. However, we can convert a mutable collection into an immutable one when required.

We’ve mentioned the handy extension functions earlier. For example, we can convert a List to Set by calling the list.toSet() method:

assertThat(inputList.toSet()).isInstanceOf(LinkedHashSet::class.java).isEqualTo(expectedSet)

Sharp eyes may have noticed that, in the assertion above, we verify the converted Set object is a LinkedHashSet instance, although we’ve called the toSet() method.

This is because Kotlin’s extension function Iterator.toSet returns an immutable LinkedHashSet instance. So, if we want to have a mutable LinkedHashSet object, toMutableSet() is the right choice.

Moreover, there are still toHashSet() and toSortedSet() methods. They allow us to obtain the desired Set type. Also, we need to note that the Set instances returned by these two methods are mutable.

4. Converting a Set Into a List

Similarly, we can use concrete List types’ constructors to convert a Set into a List. For example, we can convert a Set into an ArrayList:

val inputSet = setOf("one", "two", "three", "four", "five")
val expectedList = listOf("one", "two", "three", "four", "five")
assertThat(ArrayList(inputSet)).isEqualTo(expectedList)

Of course, we can call constructors of different types to obtain the required List type, such as LinkedList, ArrayDeque, and others.

Moreover, we should note that the List objects we get by calling their constructors are mutable.

We verify the converted ArrayList object by “isEqualTo” in the test above. We know that List‘s equals method checks both elements’ value and order. Our test passes as the setOf() method returns an immutable LinkedHashSet. That is to say, the elements’ order is fixed. However, if we convert a HashSet into a List, the elements’ order in the result List is not guaranteed.

Apart from using constructors, we can convert a Set into a List using the convenient extension function toList():

assertThat(inputSet.toList()).isEqualTo(expectedList)

The toList method returns an immutable ArrayList object. In case we need a mutable ArrayList, we can call the toMutableList method: inputSet.toMutableList().

5. Conclusion

In this article, we’ve learned how to convert between Set and List in Kotlin.

As always, the code for these examples is available over on GitHub.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.