How to convert an untyped java.util.List file to a Scala 2.8 buffer

I need to call some Java library code that returns untyped java.util.List, and I cannot convert it to a Scala 2.8 list without compiling the compiler with the following error:

[INFO] found : java.util.List[?0] where type ?0 [INFO] required: java.util.List[AnyRef] [INFO] val modules: Buffer[AnyRef] = asScalaBuffer(feedEntry.getModules) 

I tried as usual

 import scala.collection.JavaConversions._ val modules: Buffer[AnyRef] = feedEntry.getModules 

as explicit

 val modules: Buffer[AnyRef] = asScalaBuffer(feedEntry.getModules) 

I know the type of the items in the list, and I tried to set this as the buffer type, but I still get the same error.

I looked around, but all the documentation suggests that the Java list will be printed. How to convert untyped lists?

+7
source share
1 answer

I think you just need to direct it to the right type.

 val modules: Buffer[AnyRef] = feedEntry.getModules.asInstanceOf[java.util.List[AnyRef]] 

Scala can take it from there and apply the implicit conversion from JavaConversions to wrap it as a collection of Scala.

+6
source

All Articles