0

I tried to convert scala list to java list via the following snippet code:

 val obj=Array("aaa","bbb")
 val output = obj.asInstanceOf[java.util.ArrayList[String]]

But I got the following exception: Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.util.ArrayList. Please help, thanks in advance.

Jack
  • 5,540
  • 13
  • 65
  • 113

1 Answers1

1

You can't use cast because these are DIFFERENT types.

Scala provides you with classes to convert between Java <-> Scala collections

An implicit way - import scala.collection.JavaConversions._

An explicit way - import scala.collection.JavaConverters._

Also you can see my answer to a similar question here - Problems with Scala Iterator vs. Java Iterator (a casting nightmare): How to cast to Java from Scala?

AlexITC
  • 1,054
  • 1
  • 10
  • 21
Maxim
  • 7,268
  • 1
  • 32
  • 44