Kratos Kratos - 2 months ago 7
Scala Question

Type mismatch in scala list. How to get List[String] from a sequence

I have a dataframe that's reading from a MySQL table. What I eventually what after some filters are applied is to create a

List[String]
element.
Here is what I did:

val ColSelection= df.select("session","name")
var sessionData=ColSelection.filter($"session".contains(element.toString()))
var nameSequence = sessionData.select("name").collect.flatMap(_.toSeq)
val sessionList =nameSequence.toList


But with this I am getting a
List[Any]
element causing mismatches when handling it later.
How could I create straight a list that is defined to contain
String
s?
or how can I convert a
List[Any]
to list of strings?

Answer

To convert List[Any] to List[String] you could just listAny.map(_.toString)

Comments