0

I have implemented a method that is supposed to convert an array of strings into a single string. But getting an exception when using it with UDF and applying the UDF to a column: val concatUdf = udf(convertArray)

java.lang.ClassCastException: scala.collection.mutable.WrappedArray$ofRef cannot be cast to [Ljava.lang.String;

What should be improved in my current implementation in order to return valid String? I'm new to Scala and probably this is not the most elegant solution.

def convertArray: Array[String] => String =
      (strings: Array[String]) => {
        Option(strings) match {
          case Some(arr) => strings.mkString(", ")
          case Some(Array()) => ""
          case None => ""
        }
      }

3 Answers 3

1

I believe you could just do

def convertArray(strings: Array[String]): String =
  if (strings.nonEmpty)
    strings.mkString(", ")
  else
    ""

in your code, the second case is unreachable, because your first case will always match, including empty arrays. That said, your code seems to work fine for me on Scala 2.12.6 (apart from the warning about the unreachable code):

scala> def convertArray: Array[String] => String =
     |       (strings: Array[String]) => {
     |         Option(strings) match {
     |               case Some(arr) => strings.mkString(", ")
     |           case Some(Array()) => ""
     |           case None => ""
     |         }
     |       }
<console>:15: warning: unreachable code
                 case Some(Array()) => ""
                                       ^
convertArray: Array[String] => String

scala> convertArray(Array())
res1: String = ""

scala> convertArray(Array("bro"))
res2: String = bro

scala> convertArray(Array("bro", "dude"))
res3: String = bro, dude
Sign up to request clarification or add additional context in comments.

3 Comments

I'm trying to apply convertArray as a udf to a Dataframe column. val concatUdf = udf(convertArray). And getting the ClassCastException as above
@samba please edit your answer then, to add the relevant call. The error is not in your convertArray method, but in the code calling it. (You're likely not passing your method an Array[String], but something else)
Yes, just solved it. I should have used Seq: def convertArray(strings: Seq[String]): String = if (strings.nonEmpty) strings.mkString(", ") else "" Thanks for sharing your knowledge
1

Just use mkString, no need to re-invent the wheel:

 println(Array().mkString(", "))
 println(Array("hello").mkString(", "))
 println(Array("hello", "world").mkString(", "))

Output:

//empty string
hello
hello, world

Comments

0
def convertArray(strings: Array[String]): String = Option(strings).getOrElse(Array()).mkString(", ")

Output :

scala> def convertArray(strings: Array[String]): String = Option(strings).getOrElse(Array()).mkString(", ") 
convertArray: (strings: Array[String])String

scala> convertArray(Array("to", "to", "ta", "ta")) 
res16: String = to, to, ta, ta

scala> convertArray(Array()) 
res17: String = ""

scala> convertArray(null) 
res18: String = ""

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.