Questions tagged [scala-java-interop]

This pertains to calling Scala code from Java or vice-versa.

332 questions
37
votes
3 answers

How to convert a Java Stream to a Scala Stream?

As a part of an effort of converting Java code to Scala code, I need to convert the Java stream Files.walk(Paths.get(ROOT)) to Scala. I can't find a solution by googling. asScala won't do it. Any hints? Here is the related code: import static…
TeeKai
  • 671
  • 2
  • 10
  • 20
36
votes
6 answers

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

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…
Pandora Lee
  • 1,435
  • 2
  • 14
  • 17
33
votes
2 answers

How to learn about using scala.None from Java using javap?

In a previous question, Accessing scala.None from Java, it seems that people had used javap to figure out how to access scala.None from Java. I would like to know how they did that. FYI, the answer is: scala.Option$.MODULE$.apply(null); which can…
Mike Slinn
  • 7,705
  • 5
  • 51
  • 85
33
votes
3 answers

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

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…
oshyshko
  • 2,008
  • 2
  • 21
  • 31
31
votes
4 answers

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

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…
Cacho Santa
  • 6,846
  • 6
  • 41
  • 73
29
votes
6 answers

Accessing scala.None from Java

How can you access scala.None from Java? The last line causes the compiler to die with "type scala.None does not take parameters". import scala.Option; import scala.Some; import scala.None; final Option object1 = new Some("Hi…
Mike Slinn
  • 7,705
  • 5
  • 51
  • 85
29
votes
5 answers

Migrating Java to Scala

What are the most important points to be aware of, and the workarounds, when gradually migrating an existing Java codebase to Scala? With a (potentially very long) intermediate phase where both languages are in use. The sort of things I'm thinking…
Kevin Wright
  • 49,540
  • 9
  • 105
  • 155
28
votes
5 answers

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

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…
28
votes
3 answers

Using Java libraries in Scala

I am new to Scala. I am only able to write basic code thus far, but I want to start using it more concretely, rather than just learning theory. Let's say I have the following Java code in HelloWorld.java: public class HelloWorld { public static…
frazman
  • 32,081
  • 75
  • 184
  • 269
27
votes
4 answers

Convert a Java Future to a Scala Future

I have a Java Future object which I would like to convert into a Scala Future. Looking at the j.u.c.Future API, there is nothing much that I could use other than the isDone method. Is this isDone method blocking? Currently this is what I have in my…
joesan
  • 13,963
  • 27
  • 95
  • 232
26
votes
2 answers

Scala: best way to handle exceptions from Java library?

Let's say I'm using a Java library from my Scala project. That Java library throws exceptions all over the place, but I don't feel comfortable just letting them propagate "in the Scala world", since there's no way to be sure what exceptions a Scala…
Olivier Bruchez
  • 476
  • 5
  • 11
26
votes
5 answers

How do you call a Scala singleton method from Java?

I'm trying to inject some Scala code into my existing Java app. (So, being said, I want some more fun). I create a singleton stuff in Scala ScalaPower.scala package org.fun class ScalaPower object ScalaPower{ def…
Phương Nguyễn
  • 8,747
  • 15
  • 62
  • 96
25
votes
2 answers

Create an immutable list from a java.lang.Iterator

I'm using a library (JXPath) to query a graph of beans in order to extract matching elements. However, JXPath returns groups of matching elements as an instance of java.lang.Iterator and I'd rather like to convert it into an immutable scala list.…
Romain Rouvoy
  • 295
  • 1
  • 3
  • 9
24
votes
3 answers

Compatibility between Scala closures and Java 8 closures

After reading some OpenJDK mailinglist entries, it seems that the Oracle developers are currently further removing things from the closure proposal, because earlier design mistakes in the Java language complicate the introduction of the Java…
soc
  • 27,983
  • 20
  • 111
  • 215
23
votes
5 answers

java.util.Iterator to Scala list?

I have the following code: private lazy val keys: List[String] = obj.getKeys().asScala.toList obj.getKeys returns a java.util.Iterator Calling asScala, via JavaConverers (which is imported) according to the docs..…
rshepherd
  • 1,396
  • 3
  • 12
  • 21
1
2
3
22 23