scala-java-interop

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

孤街浪徒 提交于 2019-11-27 09:35:08
问题 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

Using Scala from Java: passing functions as parameters

99封情书 提交于 2019-11-27 07:33:10
Consider the following Scala code: package scala_java object MyScala { def setFunc(func: Int => String) { func(10) } } Now in Java, I would have liked to use MyScala as: package scala_java; public class MyJava { public static void main(String [] args) { MyScala.setFunc(myFunc); // This line gives an error } public static String myFunc(int someInt) { return String.valueOf(someInt); } } However, the above does not work (as expected since Java does not allow functional programming). What is the easiest workaround to pass a function in Java? I would like a generic solution that works with

How to convert a scala.List to a java.util.List?

别说谁变了你拦得住时间么 提交于 2019-11-27 06:31:07
How to convert Scala's scala.List into Java's java.util.List ? Daniel C. Sobral Scala List and Java List are two different beasts, because the former is immutable and the latter is mutable. So, to get from one to another, you first have to convert the Scala List into a mutable collection. On Scala 2.7: import scala.collection.jcl.Conversions.unconvertList import scala.collection.jcl.ArrayList unconvertList(new ArrayList ++ List(1,2,3)) From Scala 2.8 onwards: import scala.collection.JavaConversions._ import scala.collection.mutable.ListBuffer asList(ListBuffer(List(1,2,3): _*)) val x: java

How can I use a Scala singleton object in Java?

≯℡__Kan透↙ 提交于 2019-11-27 05:14:06
I have a Scala object that I need to use in a Java class. Here's the Scala object object Person { val MALE = "m" val FEMALE = "f" } How can I use this Scala object in Java? I have tried the following so far without success (compile errors): Person.MALE() //returns a String which is useless as I want the actual Person object Petr Pudlák Use Person$.MODULE$ . See also How can I pass a Scala object reference around in Java? Singletons as Synthetic classes in Scala? Edit : A working example (I checked, it compiles and works): Scala: object Person { val MALE = "m"; } Java counterpart: public class

How do you call a Scala singleton method from Java?

主宰稳场 提交于 2019-11-27 04:34:07
问题 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 showMyPower(time:Int) = { (0 to time-1).mkString(", ") } } Now, inside OldJava.java class OldJava { public void demo(){ System.out.println(?) } } What should I fill in ? so that Java will call the showMyPower method? I tried both org.fun.ScalaPower.showMyPower(10) and org.fun

What's the difference between == and .equals in Scala?

青春壹個敷衍的年華 提交于 2019-11-26 23:51:43
What is the difference between == and .equals() in Scala, and when to use which? Is the implementation same as in Java? EDIT: The related question talks about specific cases of AnyVal . The more general case is Any . Didier Dupont You normally use == , it routes to equals , except that it treats null s properly. Reference equality (rarely used) is eq . == is a final method, and calls .equals , which is not final. This is radically different than Java, where == is an operator rather than a method and strictly compares reference equality for objects. TL;DR Override equals method to compare

In Scala, how can I define a companion object for a class defined in Java?

我与影子孤独终老i 提交于 2019-11-26 23:29:14
问题 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 companion has to be defined in the same file. Is there anything I can do about this? Of course, I can define all my implicit conversions in another object and then bring it into scope, but this requires an extra import. Any other solution? 回答1: With

Iterating over Java collections in Scala

♀尐吖头ヾ 提交于 2019-11-26 21:25:46
I'm writing some Scala code which uses the Apache POI API. I would like to iterate over the rows contained in the java.util.Iterator that I get from the Sheet class. I would like to use the iterator in a for each style loop, so I have been trying to convert it to a native Scala collection but will no luck. I have looked at the Scala wrapper classes/traits, but I can not see how to use them correctly. How do I iterate over a Java collection in Scala without using the verbose while(hasNext()) getNext() style of loop? Here's the code I wrote based on the correct answer: class IteratorWrapper[A]

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

落花浮王杯 提交于 2019-11-26 20:32:23
问题 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 回答1: 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

Accessing scala.None from Java

懵懂的女人 提交于 2019-11-26 18:33:51
问题 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<String> object1 = new Some<String>("Hi there"); final Option<String> object2 = new None<String>(); This fails with "cannot find symbol constructor None()": final Option<String> object2 = new None(); This fails with "cannot find symbol variable None": final Option<String> object2 = None; In