Kotlin Tutorials

Kotlin Set associate()
Syntax & Examples

Set.associate() extension function

The associate() extension function for sets in Kotlin returns a Map containing key-value pairs provided by a transform function applied to the elements of the set.


Syntax of Set.associate()

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

fun <T, K, V> Set<T>.associate(transform: (T) -> Pair<K, V>): Map<K, V>

This associate() extension function of Set returns a Map containing key-value pairs provided by the transform function applied to elements of the given set.

Parameters

ParameterOptional/RequiredDescription
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.associate() returns value of type Map.



✐ Examples

1 Using associate() to create a map from a set of strings

In Kotlin, we can use the associate() function to create a map from a set of strings, where the keys are the strings and the values are their lengths.

For example,

  1. Create a set of strings.
  2. Use the associate() function with a transform function that returns a pair of the string and its length.
  3. 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 fruitLengths = fruits.associate { it to it.length }
    println("Fruit lengths: $fruitLengths")
}

Output

Fruit lengths: {apple=5, banana=6, cherry=6}

2 Using associate() to create a map from a set of integers

In Kotlin, we can use the associate() function to create a map 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. Use the associate() function with a transform function that returns a pair of the integer and its square.
  3. 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 squares = numbers.associate { it to it * it }
    println("Number squares: $squares")
}

Output

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

3 Using associate() with an empty set

In Kotlin, we can use the associate() function to create a map from an empty set, which will result in an empty map.

For example,

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

Kotlin Program

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

Output

Number squares in empty set: {}

Summary

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