質問

I have a requirement where I have to convert a Iterator[Long] to Iterator[String] in scala. Please let me know how can I do it

役に立ちましたか?

解決

Well just like any other collection use map. For example:

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

他のヒント

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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top