This pertains to calling Scala code from Java or vice-versa.
Questions tagged [scala-java-interop]
332 questions
12
votes
3 answers
Scala Option(null) expected as None but I got Some(0)
val i: java.lang.Integer = null
val o: Option[Int] = Option(i) // This yields Some(0)
What is the safe way to convert null: java.lang.Integer to Scala Option[Int]?

anandhu sreekumar
- 121
- 5
12
votes
2 answers
How to use Java lambdas in Scala
I have the following code:
source
.mapValues(value -> value + " Stream it!!!")
.print(Printed.toSysOut());
as you can see, mapValues expects a lambda expression.
Now, I am using Java library but the application is written in Scala. How to…

softshipper
- 32,463
- 51
- 192
- 400
11
votes
3 answers
Using scala vararg methods in java
Why do all scala vararg methods, when used from java, seem to accept a Seq of variables, and can't be used as java native vararg methods. Is this a bug?
For instance, Buffer has method def append(elems: A*): Unit. But in java it has another…

F0RR
- 1,590
- 4
- 16
- 30
11
votes
2 answers
In Scala, how can I define a companion object for a class defined in Java?
I'd like to add implicit conversions to Java classes generated by a modeling tool. So I want to add them to the companion object of those classes, so that the compiler automatically finds them. But I cannot add them in a separate file, because the…

Jean-Philippe Pellet
- 59,296
- 21
- 173
- 234
11
votes
2 answers
Implicit conversion of java.util.List to scala List does not occur
I have very basic problem with scala.collection.JavaConversions. I would expect that following code would work but implicit conversion from java.util.List[String] to scala List[String] does not happen. Why?
import collection.JavaConversions._
import…

matt
- 4,614
- 1
- 29
- 32
11
votes
2 answers
Scala getters and setters in Java class
I would like to create a Java class that follows the Scala setters/getters convention.
I tried following simple class, but it does not work:
public class JavaA {
private int a = 0;
public int a() {
return a;
}
public void a_$eq(int a) {
…

fikovnik
- 3,473
- 3
- 28
- 29
10
votes
1 answer
Implementing a Scala function in Java
I'm trying to implement a scala function object (specifically - to use with apache spark)
the equivalent java type is scala.Function0 (for a parameter less function)
besides the method that implements the business logic apply(), I see that I have to…

Ophir Yoktan
- 8,149
- 7
- 58
- 106
9
votes
1 answer
Difference in type erasure of List[Int] and List[Integer]
Why does List[scala.Int] type erase to List[Object] whilst Integer in List[java.lang.Integer] seems
to be preserved? For example, javap for
object Foo {
def fooInt: List[scala.Int] = ???
def fooInteger: List[java.lang.Integer] =…

Mario Galic
- 47,285
- 6
- 56
- 98
9
votes
5 answers
Scala extra no-arg constructor plus default constructor parameters
I am using the Scala 2.8 default parameters on a constructor, and for Java compatibility reasons, I wanted a no-arg constructor that uses the default parameters.
This doesn't work for very sensible reasons:
class MyClass(field1: String = "foo",…

jkl
- 740
- 5
- 10
9
votes
1 answer
Scala and Java futures apparently having unexpected interactions
We are using Elasticsearch 0.90.7 in our Scala Play Framework application, where the end of our "doSearch" method looks like:
def doSearch(...) = {
...
val actionRequessBuilder: ActionRequestBuilder // constructed earlier in the method
val…

Zim-Zam O'Pootertoot
- 17,888
- 4
- 41
- 69
9
votes
2 answers
Catch in Java a exception thrown in Scala - unreachable catch block
Scala doesn't have checked exceptions. However, when calling scala code from java, it's desirable to catch exceptions thrown by scala.
Scala:
def f()=
{
//do something that throws SomeException
}
Java:
try
{ f() }
catch…

loopbackbee
- 21,962
- 10
- 62
- 97
9
votes
1 answer
How can I use a Java List with Scala's foreach?
I have tried to convert a java list to a Scala list without success using asInstanceOf, as my returned list from an android call is a java list.
val apList = (wfm.getScanResults:java.util.List[ScanResult])
Wish to do this so that I can use the (new…

nfc-uk
- 696
- 3
- 7
- 15
9
votes
1 answer
Using Scala reflection with Java reflection
I have a lot of code that has been using Java reflection to invoke arbitrary user-specified methods on Java objects, as part of a DSL.
However, a lot of those Java objects, in practice, are Scala objects, and some of the methods on them are marked…

Mysterious Dan
- 1,316
- 10
- 25
9
votes
1 answer
Scala and Java Generics -- Extracting and returning nested types
Java generics can infer the type of generic type parameters based on the return type of expressions. Consider the following:
public static T uncheckedCast(Object o) {
return (T)o;
}
We can call it such as:
Map bazbogMap = new…

DK_
- 2,648
- 2
- 21
- 20
9
votes
1 answer
Using Scala type aliases from Java code
Suppose I have type alias defined in scala as
object Foo {
type Bar = Option[String]
}
It looks like I cannot refer to alias in Java code like that (it simply complains cannot find symbol):
import Foo.*;
public class Cafebabe {
void bar(Bar…

om-nom-nom
- 62,329
- 13
- 183
- 228