Convert List[List[Any]] into List[List[String]] in Scala -
i have following list[list[any]] want convert list[list[string]].
val input:list[list[any]] = list(list(1234), list(234, 678), list(8765, 90876, 1)) i looking following output:
val output:list[list[string]] = list(list(1234), list(234, 678), list(8765, 90876, 1)) i have tried doing following:
val output:list[list[string]] = input.map(_.tostring).tolist // or val output:list[list[string]] = input.map(_.tostring) none of above give me desired output.
because, list nested, need map twice.
why below not working?
val output:list[list[string]] = input.map(_.tostring) because, _ placeholder holds value of type list[string] , applying tostring method convert list string , hence result of type list[string] instead of list[list[string]].
scala> input.map(_.tostring) res2: list[string] = list(list(1234), list(234, 678), list(8765, 90876, 1)) therefore, need map input twice.
scala> input.map(_.map(_.tostring)) res0: list[list[string]] = list(list(1234), list(234, 678), list(8765, 90876, 1))
Comments
Post a Comment