Converting a Java collection into a Scala collection

后端 未结 10 1537
Happy的楠姐
Happy的楠姐 2020-11-29 17:35

Related to Stack Overflow question Scala equivalent of new HashSet(Collection) , how do I convert a Java collection (java.util.List say) into a Scala c

10条回答
  •  心在旅途
    2020-11-29 17:57

    For future reference: With Scala 2.8, it could be done like this:

    import scala.collection.JavaConversions._
    val list = new java.util.ArrayList[String]()
    list.add("test")
    val set = list.toSet
    

    set is a scala.collection.immutable.Set[String] after this.

    Also see Ben James' answer for a more explicit way (using JavaConverters), which seems to be recommended now.

提交回复
热议问题