scala-java-interop

What is the Java equivalent of a Scala object?

谁都会走 提交于 2019-11-29 06:06:37
In Scala, we can write object Foo { def bar = {} } How is this implemented by the compiler? I am able to call Foo.bar(); from Java but new Foo(); from Java gives the error cannot find symbol symbol: constructor Foo() Does the JVM support singletons natively? Is it possible to have a class in Java that does not have a constructor? Note: here is the code output by scalac -print package <empty> { final class Foo extends java.lang.Object with ScalaObject { def bar(): Unit = (); def this(): object Foo = { Foo.super.this(); () } } } Support for singletons is not on a language level, but the language

Scala convert List[Int] to a java.util.List[java.lang.Integer]

▼魔方 西西 提交于 2019-11-29 04:44:16
问题 Is there a way in Scala to convert a List[Int] to java.util.List[java.lang.Integer] ? I'm interfacing with Java (Thrift). JavaConversions supports List --> java.util.List , and implicits exist between Int --> java.lang.Integer , but from what I can tell I would still need an extra pass to manually do the conversion: val y = List(1) val z: java.util.List[Integer] = asList(y) map { (x: Int) => x : java.lang.Integer } 回答1: Apparently you need both conversions. However, you can group them in a

How to use scala.None from Java code [duplicate]

一世执手 提交于 2019-11-29 02:49:38
Possible Duplicate: Accessing scala.None from Java In Java you can create an instance of Some using the constructor, i.e. new Some(value) , but None has no partner class. How do you pass None to a Scala function from Java? I think this ugly bit will work: scala.None$.MODULE$ There is no need for a new instance since one None is as good as another... The scala.None$.MODULE$ thing doesn't always typecheck, for example this doesn't compile: scala.Option<String> x = scala.None$.MODULE$; because javac doesn't know about Scala's declaration-site variance, so you get: J.java:3: incompatible types

How can I convert Scala Map to Java Map with scala.Float to java.Float k/v conversion

痴心易碎 提交于 2019-11-29 02:10:54
问题 I would like to be able to perform the following, but it fails in the call to useMap. How can I perform this conversion? scala> import scala.collection.JavaConversions._ import scala.collection.JavaConversions._ scala> import scala.collection.JavaConverters._ import scala.collection.JavaConverters._ scala> def useMap(m: java.util.Map[java.lang.Integer, java.lang.Float]) = m useMap: (m: java.util.Map[Integer,Float])java.util.Map[Integer,Float] scala> val v: Map[Int, Float] = Map() v: Map[Int

Convert Scala Set into Java (java.util.Set)?

ⅰ亾dé卋堺 提交于 2019-11-29 01:45:53
问题 I have a Set in Scala (I can choose any implementation as I am creating the Set. The Java library I am using is expecting a java.util.Set[String]. Is the following the correct way to do this in Scala (using scala.collection.jcl.HashSet#underlying): import com.javalibrary.Animals var classes = new scala.collection.jcl.HashSet[String] classes += "Amphibian" classes += "Reptile" Animals.find(classes.underlying) It seems to be working, but since I am very new to Scala I want to know if this is

How can I convert a Java Iterable to a Scala Iterable?

烂漫一生 提交于 2019-11-28 17:23:50
Is there an easy way to convert a java.lang.Iterable[_] to a Scala.Iterable[_] ? In Scala 2.8 this became much much easier, and there are two ways to achieve it. One that's sort of explicit (although it uses implicits): import scala.collection.JavaConverters._ val myJavaIterable = someExpr() val myScalaIterable = myJavaIterable.asScala EDIT: Since I wrote this, the Scala community has arrived at a broad consensus that JavaConverters is good, and JavaConversions is bad, because of the potential for spooky-action-at-a-distance. So don't use JavaConversions at all! And one that's more like an

Java <-> Scala interop: transparent List and Map conversion

微笑、不失礼 提交于 2019-11-28 15:48:36
I am learning Scala and I have a Java project to migrate to Scala. I want to migrate it by rewriting classes one-by-one and checking that new class didn't break the project. This Java project uses lots of java.util.List and java.util.Map . In new Scala classes I would like to use Scala’s List and Map to have good-looking Scala code. The problem is that new classes (those are wtitten in Scala) do not integrate seamelessly with existing Java code: Java needs java.util.List , Scala needs its own scala.List . Here is a simplified example of the problem. There are classes Main , Logic , Dao . They

How to use Scala varargs from Java code

走远了吗. 提交于 2019-11-28 10:47:01
There are plenty of articles on calling Java varargs from Scala code, but the only thing I could find the opposite way round was this question: Using scala vararg methods in java , which doesn't have any concrete examples. I'm trying to use scala.Console from some Java code, for the reason that java.io.Console doesn't work in Eclipse, whereas the Scala one does. But I cannot get the method def readLine (text: String, args: Any*): String to work because it seems to be expecting a scala.collection.Seq[Any] for the second argument, and I don't see how to create a Seq in Java. How can I work

Convert Java List to Scala Seq

对着背影说爱祢 提交于 2019-11-28 09:44:05
I need to implement a method that return a Scala Seq , in Java. But I encounter this error: java.util.ArrayList cannot be cast to scala.collection.Seq Here is my code so far: @Override public Seq<String> columnNames() { List<String> a = new ArrayList<String>(); a.add("john"); a.add("mary"); Seq<String> b = (scala.collection.Seq<String>) a; return b; } But scala.collection.JavaConverters doesn't seem to offer the possibility to convert as a Seq . JavaConverters is what I needed to solve this. import scala.collection.JavaConverters; public Seq<String> convertListToSeq(List<String> inputList) {

Convert from scala.collection.Seq<String> to java.util.List<String> in Java code

天大地大妈咪最大 提交于 2019-11-28 08:05:57
I'm calling a Scala method, from Java. And I need to make the conversion from Seq to List. I can't modified the signature of the Scala method, so I can't used the asJavaCollection method from scala.collection.JavaConversions._ Any ideas of how can I achieve this? Using Scala 2.9.3 You're on the right track using JavaConversions , but the method you need for this particular conversion is seqAsJavaList : java.util.List<String> convert(scala.collection.Seq<String> seq) { return scala.collection.JavaConversions.seqAsJavaList(seq); } Update : JavaConversions is deprecated, but the same function can