简体   繁体   English

我可以直接在Java中使用scala List吗?

[英]Can I use scala List directly in Java?

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 List,编译器抱怨

"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中的org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding。 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代码使用它们,我有两个选择:

  1. use scala List in java directly 直接在java中使用scala List

  2. write a wrapper class that returns java.util.List for those methods. 编写一个包装器类,为这些方法返回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. 但我只是无法让scala List在Java中运行。

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();
  }

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/

The problem with this syntax: 这种语法的问题:

List xs = List(1, 2, 3);

is that it's Scala, not Java. 是Scala,而不是Java。 When you instantiate an object like that in Scala, syntactic sugar calls the apply() method of the class' companion object. 当你在Scala中实例化一个像这样的对象时,语法糖会调用类的伴随对象的apply()方法。 You would have to do that manually in Java. 您必须在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. 从Java调用Scala并不像从Scala调用Java那么有趣。 I think you'll have an easier time converting the Scala List to a Java Collection. 我认为您可以更轻松地将Scala列表转换为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 List更高阶函数都很困难,如果没有这些函数,你几乎就会拥有一个Java Collection。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM