Kotlin String associateBy()

Kotlin String associateBy() Tutorial

The associateBy() function of String class in Kotlin is used to associate each element of the original string with a key calculated from that element.

String associateBy() creates a map where each element is a value, and the corresponding keys are obtained by applying the provided transformation function to each element.

In this tutorial, we’ll explore the syntax of the associateBy() function and provide examples of its usage in Kotlin strings.

Syntax

The syntax of the associateBy() function is as follows:

fun CharSequence.associateBy(
    keySelector: (Char) -> K
): Map<K, Char>

where

ParameterDescription
keySelectorA function that takes a character and returns a key.
Parameters of String.associateBy() function

The keySelector function is applied to each character in the string, and the resulting keys are used to create a map.

You can also transform the values using the following syntax.

fun <K, V> CharSequence.associateBy(
    keySelector: (Char) -> K,
    valueTransform: (Char) -> V
): Map<K, V>

where

ParameterDescription
keySelectorA function that takes a character and returns a key.
valueTransformA function that takes a character and returns a value.
Parameters of String.associateBy() function

Examples for String associateBy() function

1. Creating a Map of Character Codes

In this example, we’ll use associateBy() to create a map of characters and their corresponding ASCII codes in a given string.

  1. Take a string value in message.
  2. Define a keySelector function that returns the ASCII code as the key for each character.
  3. Call associateBy() function on message with the keySelector function. The function returns a map of character codes.
  4. You may print the resulting map to the console output.

Kotlin Program

fun main() {
    val message = "Hello, World!"

    // Using associateBy() to create a map of character codes
    val charCodeMap = message.associateBy { char -> char.code }

    // Printing the resulting map
    println("Character Codes: $charCodeMap")
}

Output

Character Codes: {72=H, 101=e, 108=l, 111=o, 44=,, 32= , 87=W, 114=r, 100=d, 33=!}

1. Creating a Map of Character Codes with Value Transformation

In this example, we’ll use associateBy() to create a map of characters and their corresponding ASCII codes in a given string.

  1. Take a string value in message.
  2. Define a keySelector function that returns the ASCII code as the key for each character.
  3. Define a valueTransform function that returns the upper case of each character.
  4. Call associateBy() function on message with the keySelector function and valueTransform function. The function returns a map of character codes with the transformed values.
  5. You may print the resulting map to the console output.

Kotlin Program

fun main() {
    val message = "Hello, World!"

    // Using associateBy() to create a map of character codes
    val charCodeMap = message.associateBy ({ char -> char.uppercaseChar().code }, { char -> char.uppercaseChar() })

    // Printing the resulting map
    println("Character Codes: $charCodeMap")
}

Output

Character Codes: {72=H, 69=E, 76=L, 79=O, 44=,, 32= , 87=W, 82=R, 68=D, 33=!}

Summary

In this tutorial, we’ve covered the associateBy() function in Kotlin strings, its syntax, and how to use it to associate each character with a value calculated from that character. This function provides a versatile way to transform and organize string data into maps.