0

Here is the code. I get error in Step 4 when i try to convert each Array element in the List(which is the result of Step 3) into Map. What am I doing wrong here?

scala> // Step 1

scala> val inputList = List(  "data=first data || key1=r1v1 || key2=",
     |                        "data=second data || key1=r2v1 || key2=r2v2"
     |                       )
inputList: List[String] = List(data=first data || key1=r1v1 || key2=, data=second data || key1=r2v1 || key2=r2v2)

scala> // Step 2

scala> val splitted = inputList.map{ x =>
     |   x.split("\\|\\|")
     |    .map(_.trim)
     | }
splitted: List[Array[String]] = List(Array(data=first data, key1=r1v1, key2=), Array(data=second data, key1=r2v1, key2=r2v2))

scala> // Step 3

scala> val filteredList = splitted.map{ x =>
     |   val retval = for { element <- x
     |       val keyNval = element.split("=")
     |       if keyNval.size >= 2
     |     } yield {
     |       val splitted = element.split("=")
     |       val concatenated = splitted(0) + " -> " + splitted(1)
     |       concatenated
     |     }
     |   retval
     | }
warning: there were 1 deprecation warning(s); re-run with -deprecation for details
filteredList: List[Array[String]] = List(Array(data -> first data, key1 -> r1v1), Array(data -> second data, key1 -> r2v1, key2 -> r2v2))

scala> // Step 4

scala> val dnryList = filteredList.map{ x =>
     |   x.toMap
     | }
<console>:32: error: Cannot prove that String <:< (T, U).
         x.toMap
0

1 Answer 1

3

toMap requires elements of your collection to be tuples: (T, U), while elements of your filteredList are Array[String].

In "step3" you need to return tuples instead of strings, like this:

yield {
 val splitted = element.split("=")
 splitted(0) -> splitted(1)
}

filteredList is now List[Array[(String, String)]] and toMap works fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.