Scala Type Type Mismatch

I am writing methods to convert Set[Tuple2[String, String]]

to String

and vice versa.
I store the string value as v1, v2 # v3, v4 # v5, v6
To fill Set

, I split the string into ",", and in order to extract the values, I tried to split each value with "#", but I get

Mismatch Type: Found: x.type (with base type Array [String]

The code I tried to use is

val x = overwriters.split("#")
for(tuple <- x) {
  tuple.split(",")
}

      

The return type of split is a String array, so it’s not clear to me why I cannot split each member of the returned array

+3


source to share


2 answers


overwrites.split("#").map(_.split(",")).map(x=> (x(0),x(1))).toSet

      



This will lead to the same thing in a more idiomatic way.

+2


source


tuple.split(",")

returns an array of two elements. You need to convert it to a tuple.



val overwriters ="v1,v2#v3,v4#v5,v6"              
val x = overwriters.split("#").toSet
for(tuple <- x) yield {
  val t = tuple.split(",")
  (t(0),t(1))
}    

      

+2


source







All Articles