我可以在 Java 中直接使用 scala List 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4524868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I use scala List directly in Java?
提问by Visus Zhao
Can I use scala List in Java, like :
我可以在 Java 中使用 scala List,例如:
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. can't find List$.apply method.
它似乎没有编译。找不到 List$.apply 方法。
when I change it to
当我把它改成
List xs = Dir.ls()
where Dir is my scala class, and ls() returns a scala List, the compiler complaints about
其中 Dir 是我的 Scala 类,而 ls() 返回一个 Scala 列表,编译器抱怨
"Internal compiler error: java.lang.ClassCastException: org.eclipse.jdt.internal.compiler.lookup.BaseTypeBinding cannot be cast to org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding at org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.initializeTypeVariable(BinaryTypeBinding.java:927)"
“内部编译器错误:java.lang.ClassCastException:org.eclipse.jdt.internal.compiler.lookup.BaseTypeBinding 无法转换为 org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding 在 org.eclipse.jdt.internal。 compiler.lookup.BinaryTypeBinding.initializeTypeVariable(BinaryTypeBinding.java:927)"
which I have no idea what it means.
我不知道这是什么意思。
I want to write some library in scala, but would also like it to be used in Java.
我想在 Scala 中编写一些库,但也希望在 Java 中使用它。
In my scala class there are methods that return scala List, for java code to use them, I have two options:
在我的 scala 类中有返回 scala List 的方法,对于使用它们的 java 代码,我有两个选择:
use scala List in java directly
write a wrapper class that returns java.util.List for those methods.
直接在java中使用scala List
编写一个包装类,为这些方法返回 java.util.List。
I'd rather like option 1, because otherwise I'll have to write a wrapper class for nearly ALL my scala classes.
我宁愿选择选项 1,否则我将不得不为几乎所有的 Scala 类编写一个包装类。
But I just can't get scala List running in Java.
但我无法在 Java 中运行 Scala List。
采纳答案by Landei
A little java-side helper method does the trick:
一个小的java端帮助方法可以解决问题:
import scala.collection.immutable.List;
import scala.collection.immutable.List$;
import scala.collection.immutable.$colon$colon;
public class HelloScalaList {
public static void main (String[] args) {
List xs = list(1,2,3);
System.out.println(xs);
}
public static <T> List<T> list(T ... ts) {
List<T> result = List$.MODULE$.empty();
for(int i = ts.length; i > 0; i--) {
result = new $colon$colon(ts[i - 1], result);
}
return result;
}
}
[Update]
[更新]
As a result of this question, I started a little project called "Scava" in order to support calls from Java to Scala: http://code.google.com/p/scava-org/
由于这个问题,我开始了一个名为“Scava”的小项目,以支持从 Java 到 Scala 的调用:http: //code.google.com/p/scava-org/
回答by Sebastien Lorber
As of Scala 2.10, I didn't succeed with the tricks above.
从Scala 2.10 开始,我没有成功使用上述技巧。
But you can use the following code:
但是您可以使用以下代码:
public static <T> scala.collection.immutable.List<T> scalaList(List<T> javaList) {
return scala.collection.JavaConversions.asScalaIterable(javaList).toList();
}
回答by Janx
The problem with this syntax:
这种语法的问题:
List xs = List(1, 2, 3);
List xs = List(1, 2, 3);
is that it's Scala, not Java. When you instantiate an object like that in Scala, syntactic sugar calls the apply() method of the class' companion object. You would have to do that manually in Java.
是 Scala,而不是 Java。当你在 Scala 中实例化一个对象时,语法糖调用类的伴生对象的 apply() 方法。您必须在 Java 中手动执行此操作。
And you aren't actually creating a scala.collection.immutable.List (which is abstract):
而且您实际上并没有创建 scala.collection.immutable.List (这是抽象的):
scala> val list = List(1,2,3)
list: List[Int] = List(1, 2, 3)
scala> list.getClass
res12: java.lang.Class[_] = class scala.collection.immutable.$colon$colon
Calling Scala from Java isn't nearly as fun as calling Java from Scala. I think you'll have an easier time converting the Scala List to a Java Collection. Using any of the Scala List higher order functions would be difficult in Java and without those, you pretty much have a Java Collection anyway.
从 Java 调用 Scala 并不像从 Scala 调用 Java 那样有趣。我认为您可以更轻松地将 Scala 列表转换为 Java 集合。在 Java 中使用任何 Scala List 高阶函数都是困难的,如果没有这些,无论如何你几乎都有一个 Java 集合。