如何在scala中将Iterator [Long]转换为Iterator [String]

时间:2014-03-10 05:59:24

标签: scala scala-collections

我有一个要求,我必须在scala中将Iterator [Long]转换为Iterator [String]。 请让我知道我该怎么做

由于

2 个答案:

答案 0 :(得分:1)

就像任何其他集合一样使用map。例如:

scala> val ls = List(1,2,3).toIterator
ls: Iterator[Int] = non-empty iterator

scala> ls.map(_.toString) //it was map(x+""). see comments on why it is bad
res0: Iterator[String] = non-empty iterator

scala> res0.next
res1: String = 1

scala> res0.next
res2: String = 2

scala> res0.next
res3: String = 3

答案 1 :(得分:1)

scala> List(1,2,3).toIterator.map(_.toString)
res1: Iterator[String] = non-empty iterator

scala> List(1,2,3).toIterator.map(_.toString).mkString(", ")
res2: String = 1, 2, 3