0

我在 JVM 中创建了一个 hashmap 并想在 jpype.shutdownJVM 之后访问它,我该怎么做?

我知道 dict 在 python 中用作 hashmap,我尝试过:

jpype.startJVM(jpype.getDefaultJVMPath(),"-Djava.class.path=%s" % classpath) hashmap = jpype.java.util.HashMap() ...#insert pairs jpype.shutdownJVM() ... hashmap ["key"] 这不起作用。

谢谢。

4

1 回答 1

0

密钥访问将映射到 java 调用。由于您已经关闭了 jvm,这将不起作用。

您必须首先从 Java HashMap 创建一个新的 python 目录。确保这些值不是复杂的 Java 对象并且可以自动翻译

import jpype
jpype.startJVM(jpype.getDefaultJVMPath())
hashmap = jpype.java.util.HashMap()
# insert pairs
hashmap.put("foo", "bar")
# normally newmap = dict(hashmap) should work but jpype
# doesn't seem to support this...
newmap = {}
for i in hashmap:
    # you might have to map more complex keys or values to python objects
    # before putting them into newmap
    newmap[i] = hashmap[i]
jpype.shutdownJVM()
print newmap["foo"]
于 2014-10-09T16:12:01.100 回答