How to combine all keys and values from a map into a single string in Kotlin

1 Answer

0 votes
fun combineKeysAndValues(map: Map<String, String>): String {
    // Combine keys and values into a single string
    return map.entries.joinToString(", ") { "${it.key}=${it.value}" }
}

fun main() {
    val map = mapOf(
        "Key1" to "Value1",
        "Key2" to "Value2",
        "Key3" to "Value3",
        "Key4" to "Value4"
    )

    val result = combineKeysAndValues(map)

    println("Combined keys and values: $result")
}
 
   
      
/*
run:
   
Combined keys and values: Key1=Value1, Key2=Value2, Key3=Value3, Key4=Value4
  
*/

 



answered Apr 1, 2025 by avibootz

Related questions

3 answers 174 views
2 answers 89 views
1 answer 92 views
1 answer 100 views
3 answers 108 views
...