Kotlin Set associateByTo()
Syntax & Examples


Set.associateByTo() extension function

The associateByTo() extension function for sets in Kotlin populates and returns the destination mutable map with key-value pairs, where the key is provided by the keySelector function applied to each element of the set. Optionally, the value can be provided by the valueTransform function.


Syntax of Set.associateByTo()

There are 2 variations for the syntax of Set.associateByTo() extension function. They are:

1.
fun <T, K, M : MutableMap<in K, in T>> Set<T>.associateByTo(destination: M, keySelector: (T) -> K): M

This extension function populates and returns the destination mutable map with key-value pairs, where the key is provided by the keySelector function applied to each element of the given set and the value is the element itself.

Returns value of type M.

2.
fun <T, K, V, M : MutableMap<in K, in V>> Set<T>.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M

This extension function populates and returns the destination mutable map with key-value pairs, where the key is provided by the keySelector function and the value is provided by the valueTransform function applied to elements of the given set.

Returns value of type M.



✐ Examples

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

In Kotlin, we can use the associateByTo() 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 associateByTo() function with a keySelector function that returns the first character of the string.
  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.associateByTo(fruitMap) { it.first() }
    println("Fruit map: $fruitMap")
}

Output

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

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

In Kotlin, we can use the associateByTo() 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 associateByTo() function with a keySelector function that returns the integer and a valueTransform function that returns the square of the integer.
  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.associateByTo(squaresMap, { it }, { it * it })
    println("Number squares: $squaresMap")
}

Output

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

3 Using associateByTo() with an empty set

In Kotlin, we can use the associateByTo() 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 associateByTo() function with a keySelector function that returns the integer and a valueTransform function that returns the square of the integer.
  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.associateByTo(squaresMap, { it }, { it * it })
    println("Number squares in empty set: $squaresMap")
}

Output

Number squares in empty set: {}

Summary

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