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

1 Answer

0 votes
object CombineKeysAndValues {
  // Function to combine keys and values into a single string
  def combineKeysAndValues(map: Map[String, String]): String = {
    map.map { case (key, value) => s"$key=$value" }
       .mkString(", ")
  }

  def main(args: Array[String]): Unit = {
    val map: Map[String, String] = Map(
      "Key1" -> "Value1",
      "Key2" -> "Value2",
      "Key3" -> "Value3",
      "Key4" -> "Value4"
    )

    val result = combineKeysAndValues(map)

    println(s"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 254 views
3 answers 112 views
1 answer 125 views
1 answer 102 views
1 answer 98 views
3 answers 142 views
...