scalascala-collections

how to convert a Iterator[Long] to Iterator[String] in scala


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


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