Kotlin Set associateBy()
Syntax & Examples


Set.associateBy() extension function

The associateBy() extension function for sets in Kotlin returns a Map containing the elements of the set indexed by the key returned from the keySelector function. It can also return a Map containing values provided by the valueTransform function and indexed by the keySelector function.


Syntax of Set.associateBy()

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

1.
fun <T, K> Set<T>.associateBy(keySelector: (T) -> K): Map<K, T>

This extension function returns a Map containing the elements from the given set indexed by the key returned from the keySelector function applied to each element.

Returns value of type Map<K, T>.

2.
fun <T, K, V> Set<T>.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map<K, V>

This extension function returns a Map containing the values provided by the valueTransform and indexed by keySelector functions applied to elements of the given set.

Returns value of type Map<K, V>.



✐ Examples

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

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

Output

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

2 Using associateBy() with valueTransform to create a map from a set of integers

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

Output

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

3 Using associateBy() with an empty set

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

Output

Number squares in empty set: {}

Summary

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