简体   繁体   English

如何从Java调用Scala的HashMap.toArray()?

[英]How to call Scala's HashMap.toArray() from Java?

I use scala.collection.immutable.HashMap<A,B> from some Java code and would like to use the Scala-native method toArray() to convert the contents (or values) of the map to an array. 我从一些Java代码中使用scala.collection.immutable.HashMap<A,B> ,并希望使用Scala-native方法toArray()将映射的内容(或值)转换为数组。

I currently use JavaConversions.asMap() etc, and then use the traditional, but ugly, Java toArray(T[]) methods but I would prefer to call the Scala built in method directly instead. 我目前使用JavaConversions.asMap()等,然后使用传统但丑陋的Java toArray(T[])方法,但我更愿意直接调用Scala内置方法。

This must be done from Java. 这必须从Java完成。 Rewriting the code in Scala is not an option. 在Scala中重写代码不是一种选择。

I am using Scala 2.9.1. 我正在使用Scala 2.9.1。

You need to supply a ClassManifest for the array type, T . 您需要为数组类型T提供ClassManifest This is available from the companion object (see note) for ClassManifest . 可以从ClassManifest的配套对象(参见注释)获得。 So: 所以:

itr.toArray(ClassManifest$.MODULE$.fromClass(T.class));

In this example, T is a real type (not a type parameter). 在此示例中, T是实数类型(不是类型参数)。 So for example, if itr were a Seq[String] , you would use this; 所以例如,如果itr是一个Seq[String] ,你会使用它;

itr.toArray(ClassManifest$.MODULE$.fromClass(String.class));

Because scala's Map is actually a bunch of tuples, you would use this: 因为scala的Map实际上是一堆元组,你会使用这个:

map.toArray(ClassManifest$.MODULE$.fromClass(Tuple2.class));

Of course, this gives you a Tuple2[] , rather than a Tuple2<K,V>[] for the key and values types K and V respectively. 当然,这为键和值类型KV分别提供了一个Tuple2[] ,而不是Tuple2<K,V>[] As you are in Java-land, you can cast the raw type 当你在Java-land中时,可以转换原始类型


Note : accessing the companion object from Java . 注意从Java访问配套对象

The companion object of a type M is available by accessing the static field M$.MODULE$ 通过访问静态字段M$.MODULE$ ,可以获得类型M的伴随对象

这应该足够了:

map.toArray(scala.reflect.ClassManifest$.MODULE$.Object);

从API文档来看,似乎您需要提供类型为Manifest (或ArrayTag ,取决于版本)to toArray

calling scala specific methods from java can sometimes be very ugly like in this case. 从java调用scala特定方法有时会非常难看,就像在这种情况下一样。 I don't know if there is a better solution to this, but this should work: 我不知道是否有更好的解决方案,但这应该有效:

import scala.collection.immutable.HashMap;

import scala.Tuple2;
import scala.reflect.ClassManifest$;
import scala.reflect.ClassManifest;

public class Foo {
  public static void main(String[] args) {
    HashMap<String,String> map = new HashMap();
    Object ar = map.<Tuple2<String,String>>toArray((ClassManifest<Tuple2<String,String>>)ClassManifest$.MODULE$.fromClass(new Tuple2<String,String>("","").getClass()));
    System.out.println(ar.toString());
  }
}

I don't know of a way in Java to get Class<Tuple2<String,String>> without instantiating it first. 我不知道在Java中如何获得Class<Tuple2<String,String>>而不首先实例化它。 In scala I would use classOf[...] is there an equivalent in java? 在scala中我会使用classOf[...]在java中是否存在等价物?

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

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