How to Deserialize java.util.ArrayList from Scala

Problem

We have a Java object, in my case java.util.ArrayList of strings, serialized with native Java serializer and base-64 encoded. I need to get out the array of strings in Scala.

Solution

import java.io._
import java.util.{ArrayList, Base64}

val arrayListString = "rO0..."

// get data bytes from base-64 encoded string
val arBytes = Base64.getDecoder.decode(arrayListString)

// use standard Java object serializer
val arOis = new ObjectInputStream(new ByteArrayInputStream(arBytes))

//deserialize
val arDes = arOis.readObject().asInstanceOf[ArrayList[String]]

Have a question⁉ Contact me.