How to convert a java.util.List to a Scala list

boycod3 picture boycod3 · Apr 23, 2013 · Viewed 67.7k times · Source

I have this Scala method with below error. Cannot convert into a Scala list.

 def findAllQuestion():List[Question]={
   questionDao.getAllQuestions()
 } 

type mismatch; found : java.util.List[com.aitrich.learnware.model.domain.entity.Question] required: scala.collection.immutable.List[com.aitrich.learnware.model.domain.entity.Question]

Answer

Fynn picture Fynn · Apr 23, 2013

You can simply convert the List using Scala's JavaConverters:

import scala.collection.JavaConverters._

def findAllQuestion():List[Question] = {
  questionDao.getAllQuestions().asScala
}