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
Parameter | Description |
---|---|
keySelector | A function that takes a character and returns a key. |
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
Parameter | Description |
---|---|
keySelector | A function that takes a character and returns a key. |
valueTransform | A function that takes a character and returns a value. |
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.
- Take a string value in
message
. - Define a
keySelector
function that returns the ASCII code as the key for each character. - Call
associateBy()
function onmessage
with thekeySelector
function. The function returns a map of character codes. - 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.
- Take a string value in
message
. - Define a
keySelector
function that returns the ASCII code as the key for each character. - Define a
valueTransform
function that returns the upper case of each character. - Call
associateBy()
function onmessage
with thekeySelector
function andvalueTransform
function. The function returns a map of character codes with the transformed values. - 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.