This pertains to calling Scala code from Java or vice-versa.
Questions tagged [scala-java-interop]
332 questions
23
votes
8 answers
Convert Scala Option to Java Optional
I need to convert Scala Option to Java Optional. I managed to wrote this:
public Optional convertOption2Optional(Option option) {
return option.isDefined() ? Optional.of(option.get()) : Optional.empty();
}
But I don't like it.
Is…

mjjaniec
- 793
- 1
- 8
- 19
22
votes
5 answers
Automatically convert Scala code to Java code
I have an app written in Scala and some of my team members want a Java version of it. It is a demo app to use another API written in Scala, and they want a Java version of the app to be able to use the API from Java. However, the app is somewhat…

Jus12
- 17,824
- 28
- 99
- 157
21
votes
2 answers
Convert Scala Any to Java Object
I've a problem using Java Reflection from Scala. My Code:
case class MyClass(id: String, value: Double)
def create(values: Map[String, Any]): MyClass = {
val constructor = classOf[MyClass].getConstructors.head
val arguments =…

Torben
- 1,290
- 5
- 22
- 41
20
votes
5 answers
Please explain use of Option's orNull method
Scala's Option class has an orNull method, whose signature is shown below.
orNull [A1 >: A](implicit ev : <:<[Null, A1]) : A1
I'm bewildered by the implicit thing. Would somebody please explain how it can be used, ideally with an example?

David
- 5,184
- 3
- 41
- 67
19
votes
1 answer
How to use Scala varargs from Java code
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…

Luigi Plinge
- 50,650
- 20
- 113
- 180
17
votes
2 answers
Scala - ambiguous reference to overloaded definition -- with varargs
Possible Duplicate:
How do I disambiguate in Scala between methods with vararg and without
I am currently porting part of an application to scala and it uses the Oval library. The method is question is the Validator.validate method. It has two…

Travis Stevens
- 2,198
- 2
- 17
- 25
17
votes
3 answers
Convert scala future to java future
I have a generated java interface containing a method:
public Future> getCustomersAsync(AsyncHandler asyncHandler);
I want to implement it using Akka. I wrote following:
override def getCustomerAsync(asyncHandler:…

mirelon
- 4,896
- 6
- 40
- 70
16
votes
1 answer
Time complexity of JavaConverters asScala method
Starting with Scala version 2.9 there exists a handy converter to convert from java.util.List and other collections to Scala's data structures by writing something like this:
import scala.collection.JavaConverters._
def scalaVersion =…

Frank
- 10,461
- 2
- 31
- 46
15
votes
3 answers
Idiomatic use of a Java comparable object
I'm using some java.util.Date (which implements java.lang.Comparable) and would like to be able to use it nicely, e.g. use < and >= instead of "compareTo(other) == 1". Is there a nice way to just easily mix in something like scala.math.Ordered…

Heptic
- 3,076
- 4
- 30
- 51
15
votes
2 answers
Scala: Overriding Generic Java Methods II
In Scala, I need to override the following, given, Java classes and methods:
public abstract class AbstractJava {
protected abstract T test(Class extends T> clazz);
}
public class ConcreteJava extends AbstractJava

robbbert
- 2,183
- 15
- 15
14
votes
3 answers
Can I use scala List directly in Java?
Can I use scala List in Java, like :
import scala.collection.immutable.List;
class HelloScalaList {
public static void main (String[] args) {
List xs = List(1, 2, 3);
System.out.println(xs);
}
}
It does not seem to compile.…

Visus Zhao
- 1,144
- 2
- 12
- 25
13
votes
3 answers
What is the Java equivalent of a Scala object?
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…

Jus12
- 17,824
- 28
- 99
- 157
13
votes
2 answers
Abstract methods and the varargs annotation
Scala provides a @varargs annotation that generates a Java varargs forwarder method, which makes it possible to write something like this:
import scala.annotation.varargs
class Foo {
@varargs def foo(args: String*): Unit = {
…

Travis Brown
- 138,631
- 12
- 375
- 680
13
votes
6 answers
Getting a Scala Map from a Java Properties
I was trying to pull environment variables into a scala script using java Iterators and / or Enumerations and realised that Dr Frankenstein might claim parentage, so I hacked the following from the ugly tree instead:
import…

Don Mackenzie
- 7,953
- 7
- 31
- 32
13
votes
1 answer
What's the difference between a class with a companion object and a class and object with the same name?
A Scala class's "companion object" can be viewed as a singleton object with the same fully qualified name as the class (i.e. same name, in same package). They are used to hold utility functions common to all instances of the class, as a replacement…

Mechanical snail
- 29,755
- 14
- 88
- 113