Question

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

Était-ce utile?

La solution

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

Autres conseils

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top