Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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]

like image 597
boycod3 Avatar asked Apr 23 '13 06:04

boycod3


2 Answers

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

import scala.collection.JavaConverters._  def findAllQuestion():List[Question] = {   questionDao.getAllQuestions().asScala.toList } 
like image 159
Fynn Avatar answered Sep 20 '22 13:09

Fynn


import scala.collection.JavaConversions._ 

will do implicit conversion for you; e.g.:

var list = new java.util.ArrayList[Int](1,2,3) list.foreach{println} 
like image 33
Neil Avatar answered Sep 21 '22 13:09

Neil