Scala tuple for string

Suppose I have a list of tuples

('a', 1), ('b', 2)... 

How can I get it converted to a string in the format

 a 1 b 2 

I tried using collection.map(_.mkString('\t')) however, I get an error, since essentially I am applying the operation to a tuple instead of a list. Using flatMap did not help

+8
scala
source share
1 answer

For Tuple2 you can use:

 val list = List(("1", 4), ("dfg", 67)) list.map { case (str, int) => s"$str $int"} 

For any tuples, try this code:

 val list = List[Product](("dfsgd", 234), ("345345", 345, 456456)) list.map { tuple => tuple.productIterator.mkString("\t") } 
+20
source share

All Articles