Scala 元组到字符串(使用 mkString) [英] Scala Tuple to String (using mkString)
本文介绍了Scala 元组到字符串(使用 mkString)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个元组列表
('a', 1), ('b', 2)...
如何将其转换为格式的字符串
How would one get about converting it to a String in the format
a 1
b 2
我尝试使用 collection.map(_.mkString('\t'))
但是我收到了一个错误,因为我基本上是将操作应用于元组而不是列表.使用 flatMap
也没有帮助
I tried using collection.map(_.mkString('\t'))
However I'm getting an error since essentially I'm applying the operation to a tuple instead of a list. Using flatMap
didn't help either
推荐答案
对于 Tuple2
你可以使用:
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")
}
这篇关于Scala 元组到字符串(使用 mkString)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文