2

I have java API which return this type:

ArrayList[ArrayList[String]] = Foo.someJavaMethod()   

In scala program, I need to send above type as a parameter to a scala function 'bar' whose type is

def bar(param: List[List[String]]) : List[String] = {

}

so I call bar like:

val list = bar(Foo.someJavaMethod())

but this does not work as I get compile error.

I thought have this import

import scala.collection.JavaConversions._ 

will do implicit automatic conversion between Java and Scala collections.

I also tried using like:

Foo.someJavaMethod().toList 

but that does not work either.

What is the solution to this problem?

1 Answer 1

7

First, ArrayList does not convert to List, it converts to a Scala Buffer. Second, implicit conversion will not recurse into the elements of your collections.

You'll have to manually map the inner lists. Either with implicit conversions:

import collection.JavaConversions._
val f = Foo.someJavaMethod()
bar(f.toList.map(_.toList))

Or, more explicitly, if you prefer:

import collection.JavaConverters._
val f = Foo.someJavaMethod()
bar(f.asScala.toList.map(_.asScala.toList))
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.