115

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]

giampaolo
  • 6,906
  • 5
  • 45
  • 73
boycod3
  • 5,033
  • 11
  • 58
  • 87
  • 3
    Folks looking at this question might find this other question of interest: https://stackoverflow.com/questions/8301947/what-is-the-difference-between-javaconverters-and-javaconversions-in-scala – Amndeep7 Apr 22 '17 at 23:53

5 Answers5

141

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

import scala.collection.JavaConverters._

def findAllQuestion():List[Question] = {
  questionDao.getAllQuestions().asScala.toList
}
joelittlejohn
  • 11,665
  • 2
  • 41
  • 54
Fynn
  • 4,753
  • 3
  • 32
  • 69
  • 2
    This seems working.. def findAllStudentTest(): List[StudentTest] = { var list = studentTestDao.getAllStudentTests() var slist = list.asScala.toList slist } – boycod3 Apr 24 '13 at 09:34
  • 14
    a java.util.List .asScala comes back as a buffer for me oddly. – phillro Jan 17 '14 at 17:52
  • 2
    Me too: ` scala> import scala.collection.JavaConverters._ ` import scala.collection.JavaConverters._ scala> val l = new java.util.ArrayList[java.lang.String] l: java.util.ArrayList[String] = [] scala> l.add("hi") res70: Boolean = true scala> l.add("de") res71: Boolean = true scala> l.asScala res72: scala.collection.mutable.Buffer[String] = Buffer(hi, de)` – critium Apr 18 '14 at 16:23
  • a scala `Buffer` is a java `List`. Everything is fine :) – mauhiz May 07 '14 at 05:23
  • 3
    @mauhiz If a Scala `Buffer` is a Java `List`, then *nothing* is fine! The OP wanted a conversion from Java List to an immutable Scala List. Expressions that expect a Scala `List` won't typecheck with a `Buffer`. – Andres F. Jul 15 '16 at 18:58
  • 6
    you forgot to add `.toList` so the code is `questionDao.getAllQuestions().asScala.toList` – Raymond Chenon Oct 13 '17 at 15:15
  • 2
    Just write `asScala.toList` and it will convert to Scala list – kosiara - Bartosz Kosarzycki Jan 24 '18 at 18:33
76
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}
Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
Neil
  • 7,482
  • 6
  • 50
  • 56
  • 12
    `scala.collection.JavaConversions._` is deprecated in favor of `scala.collection.JavaConverters._` so [this answer](https://stackoverflow.com/a/16164163/918858) is better – Dexter Legaspi May 16 '18 at 14:24
  • 4
    JavaConverts has been deprecated in favor of: `scala.jdk.CollectionConverters._` as of 2.13. Details can be found [here](https://stackoverflow.com/a/55408424/63013). – Steven Levine Jul 28 '20 at 01:48
32
def findAllStudentTest(): List[StudentTest] = { 
  studentTestDao.getAllStudentTests().asScala.toList
} 
Cory Klein
  • 51,188
  • 43
  • 183
  • 243
boycod3
  • 5,033
  • 11
  • 58
  • 87
  • 3
    Why the `var`s? Also, it seems that the last two lines could be combined to a single line `list.asScala.toList` – redent84 Oct 20 '14 at 16:38
  • the first var list will get java util list and the second slist will convert java util list to scala list. – boycod3 Oct 27 '14 at 07:38
  • 4
    I mean, why `var` instead of `val` if you're not modifying the variable – redent84 Oct 27 '14 at 09:09
  • But in the case sub operations ,wee need to use var instead of val. – boycod3 Dec 01 '14 at 09:50
  • Both the `var list` and `val slist` were unnecessary. I think they made this answer more complicated, so I consolidated them and made the answer more simple. @jijeshvu07 If you disagree, I would be happy to undo the edit and submit this change instead as its own answer. – Cory Klein Jan 21 '15 at 18:09
  • The `.asScala` did not seem to be recognized any more (Scala 2.12). Just `.toList` or `.toSeq` is enough. – akauppi Jan 13 '17 at 09:29
21

Starting Scala 2.13, the package scala.collection.JavaConverters is marked as deprecated in favor of scala.jdk.CollectionConverters:

import scala.jdk.CollectionConverters._

// val javaList: java.util.List[Int] = java.util.Arrays.asList(1, 2, 3)
javaList.asScala.toList
// List[Int] = List(1, 2, 3)
Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190
8

Import JavaConverters , the response of @fynn was missing toList

import scala.collection.JavaConverters._

def findAllQuestion():List[Question] = {
  //           java.util.List -> Buffer -> List
  questionDao.getAllQuestions().asScala.toList
}
Raymond Chenon
  • 11,482
  • 15
  • 77
  • 110