Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert java.util.Set to scala.collection.Set

Tags:

How can I convert a java.util.Set[String] to a scala.collection.Set with a generic type in Scala 2.8.1?

import scala.collection.JavaConversions._  var in : java.util.Set[String] = new java.util.HashSet[String]()  in.add("Oscar") in.add("Hugo")  val out : scala.collection.immutable.Set[String] = Set(in.toArray : _*) 

And this is the error message

<console>:9: error: type mismatch;   found   : Array[java.lang.Object] required: Array[_ <: String]    val out : scala.collection.immutable.Set[String] = Set(javaset.toArray : _*) 

What am I doing wrong?

like image 797
Twistleton Avatar asked May 26 '11 19:05

Twistleton


People also ask

How to convert Java Set to Scala Set?

A Java Set can be converted to a Scala Set by importing JavaConversions. asScalaSet method. Here, we need to call asScalaSet method which has a java Set as its argument. Therefore, this method returns a Scala Set.

Can we convert Scala code to Java?

Decompile Scala code to Java In the Project tool window, right-click a Scala library class that you want to decompile. From the context menu, select Decompile Scala to Java. IntelliJ IDEA converts code to Java and opens the converted file in the editor.


1 Answers

Use JavaConverters instead

import scala.collection.JavaConverters._  val out = in.asScala  out: scala.collection.mutable.Set[String] = Set(Hugo, Oscar) 
like image 55
oluies Avatar answered Oct 10 '22 21:10

oluies