English 中文(简体)
Java: 从文件中保存和读取 hashmaps
原标题:Java: saving and reading hashmaps from file
  • 时间:2012-05-28 05:16:27
  •  标签:
  • java
  • hashmap

我有以下嵌巢的哈马发作:

HashMap<Foo1, HashMap<Foo2, TObjectDoubleHashMap<Foo1>>> my_map;

tobjectDoubleHashmap 中有超过100万个条目, 并且需要大约一分钟的时间生成。 我有什么选项保存它以存档, 这样它不需要每次生成?

到目前为止,我把实际的键和值保存到文本文件中,然后再将其装入哈沙马图,但这并没有节省太多时间。 有没有一个更快的方法,比如用序列保存它作为使用序列的物体? 如果是这样,我该怎么做才能使我的钥匙可以连载?

最佳回答

< 坚固> 是否有一种更快的方法, 例如使用序列化来将它保存为对象?

地图的序列化只是写出密钥和匹配值。使用序列化的速度不会比自己做长手的要快。 Hashmap 系统对序列化数据的描述是:

The capacity of the HashMap (the length of the bucket array) is emitted (int), followed by the size (an int, the number of key-value mappings), followed by the key (Object) and value (Object) for each key-value mapping. The key-value mappings are emitted in no particular order.

您可以看到代码",http://grepcode.com/files/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/HashMap.java#HashMap.writeObject%28java.io.Object OutputStream%29" rel=“no follow'这里

「强」 我要做什么才能让我的钥匙连号?

要使物体序列可被引用,请执行 Sretariable 接口。您可能还要指定 siralVersionUID 。添加一个字段:

private static final long serialVersionUID = 1L;
问题回答

您是否使用 Map< Foo1, Map< Foo2, 价值 & gt; & gt; , 因为您想要对每个值使用两个密钥?

如果您可以尝试使用 Guava s < a href=" http://docs.guava-librarys.googlecode.com/git-history/v12.0javadoc/com/google/common/communal/sarm/Cable.html" rel=“nofollow” > Table





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签