Kotlin Set associateTo()
Syntax & Examples


Set.associateTo() extension function

The associateTo() extension function for sets in Kotlin populates and returns the destination mutable map with key-value pairs provided by the transform function applied to each element of the set.


Syntax of Set.associateTo()

The syntax of Set.associateTo() extension function is:

fun <T, K, V, M : MutableMap<in K, in V>> Set<T>.associateTo(destination: M, transform: (T) -> Pair<K, V>): M

This associateTo() extension function of Set populates and returns the destination mutable map with key-value pairs provided by the transform function applied to each element of the given set.

Parameters

ParameterOptional/RequiredDescription
destinationrequiredThe mutable map to which key-value pairs will be added.
transformrequiredA function that takes an element of the set and returns a Pair of key and value to be included in the resulting Map.

Return Type

Set.associateTo() returns value of type M.



✐ Examples

1 Using associateTo() to populate a mutable map with elements of a set of strings

In Kotlin, we can use the associateTo() function to populate a mutable map with elements from a set of strings, where the keys are the first characters of the strings and the values are the strings themselves.

For example,

  1. Create a set of strings.
  2. Create an empty mutable map.
  3. Use the associateTo() function with a transform function that returns a pair of the first character of the string and the string itself.
  4. Print the resulting map to the console using the println function.

Kotlin Program

fun main(args: Array<String>) {
    val fruits = setOf("apple", "banana", "cherry")
    val fruitMap = mutableMapOf<Char, String>()
    fruits.associateTo(fruitMap) { it.first() to it }
    println("Fruit map: $fruitMap")
}

Output

Fruit map: {a=apple, b=banana, c=cherry}

2 Using associateTo() to populate a mutable map with elements of a set of integers

In Kotlin, we can use the associateTo() function to populate a mutable map with elements from a set of integers, where the keys are the integers and the values are their squares.

For example,

  1. Create a set of integers.
  2. Create an empty mutable map.
  3. Use the associateTo() function with a transform function that returns a pair of the integer and its square.
  4. Print the resulting map to the console using the println function.

Kotlin Program

fun main(args: Array<String>) {
    val numbers = setOf(1, 2, 3, 4, 5)
    val squaresMap = mutableMapOf<Int, Int>()
    numbers.associateTo(squaresMap) { it to it * it }
    println("Number squares: $squaresMap")
}

Output

Number squares: {1=1, 2=4, 3=9, 4=16, 5=25}

3 Using associateTo() with an empty set

In Kotlin, we can use the associateTo() function to populate a mutable map with elements from an empty set, which will result in an empty map.

For example,

  1. Create an empty set of integers.
  2. Create an empty mutable map.
  3. Use the associateTo() function with a transform function that returns a pair of the integer and its square.
  4. Print the resulting map to the console using the println function.

Kotlin Program

fun main(args: Array<String>) {
    val emptySet = emptySet<Int>()
    val squaresMap = mutableMapOf<Int, Int>()
    emptySet.associateTo(squaresMap) { it to it * it }
    println("Number squares in empty set: $squaresMap")
}

Output

Number squares in empty set: {}

Summary

In this Kotlin tutorial, we learned about associateTo() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.