1. Overview

A Set is an unordered collection of unique elements while a List is an ordered collection of elements. Both are used in programming to solve a myriad of computational problems.

In this tutorial, we’re going to use both data structures to explore various ways of adding List contents into a Set in Kotlin.

2. Using a for Loop

A straightforward way to transfer List contents into a Set is by using a for loop to iterate over the List and add each element to the Set:

fun addListToSet(list: List<String>): Set<String> {
    val set = mutableSetOf<String>()
    for (element in list) {
        set.add(element)
    }
    return set
}

As mentioned earlier, this method iterates the elements of the List and adds each element into a Set using the Set‘s add() method. Finally, this method returns the set containing all the elements from the list.

To be sure this method works correctly, let’s test it:

@Test
fun `test adding list contents to set programmatically`() {
    val list = listOf("apple", "banana", "orange")
    val set = addListToSet(list)
    assertEquals(setOf("apple", "banana", "orange"), set)
}

The unit test above uses the addListToSet() method to add the elements of a List into a Set. Finally, we assert that the newly created Set contains the correct elements.

3. Using the Set Interface’s addAll() Method

Another way to add List contents into a Set is to use the addAll() method. This method adds all the elements of the List to the mutable Set:

@Test
fun `test adding list contents to set using addAll() method`() {
    val list = listOf("apple", "banana", "orange")
    val set = mutableSetOf<String>()
    set.addAll(list)
    assertEquals(setOf("apple", "banana", "orange"), set)
}

In this code sample, we create a mutable Set using the mutableSetOf() method. Then, we call the addAll() method on the Set and pass the List as an argument.

4. Using the List Interface’s toSet() Method

A third way to add List elements into a Set is to use the toSet() method. This method returns a Set containing all the elements from a List:

@Test
fun `test adding list contents to set using toSet() method`() {
    val list = listOf("apple", "banana", "orange")
    val set = list.toSet()
    assertEquals(setOf("apple", "banana", "orange"), set)
}

In this example, we declare a List list and call the toSet() method on it to obtain a new Set containing all the elements from the List.

5. Using the plus() Operator

Alternatively, we can also make use of the plus() operator to transfer List contents to a Set:

@Test
fun `test adding list contents to set using plus operator`() {
    val set = setOf<String>()
    val list = listOf("apple", "banana", "orange")
    val newSet = set + list
    assertEquals(setOf("apple", "banana", "orange"), newSet)
}

Here, we declare an empty Set and a List of String elements. We then create a new Set by concatenating the empty Set and the List using the plus() operator.

6. Using the union() Method

Alternatively, we can obtain the same goal by using the union() method in place of the plus() operator:

@Test
fun `test adding list contents to set using union method`() {
    val set = setOf<String>()
    val list = listOf("apple", "banana", "orange")
    val newSet = set.union(list)
    assertEquals(setOf("apple", "banana", "orange"), newSet)
}

7. Conclusion

In this article, we’ve explored various ways of adding List contents into a Set in Kotlin. First, we discussed a programmatic approach that simply iterates a List and adds each element to a Set. Furthermore, we looked at other approaches that make use of Kotlin in-built methods such as addAll() and toSet(). We finished up with the use of the plus operator and the union method to concatenate an empty Set with the elements of a List.

As always, the code used in this article 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.