java - 如何在文件中序列化/保存非常大的 HashMap ,并知道文件中每个键的位置?

标签 java serialization hashmap

我有一个大HashMap <String, List<String>>我想保存在一个文件中。我不想用Java的默认方法序列化它,因为它们还存储了很多我不需要的东西,例如类的信息(基本上我只想要字符串)。我还想知道每个 key 存储在文件中的位置,这样我就不必查找整个文件来找到它。 (文件/ HashMap 太大而无法将其全部保存在内存中)。我的想法是循环遍历文件,只计算用于写入此键和值对的字节数,并将它们的确切位置存储在格式为 <String, Long> 的 HashMap 中。 .

例如,假设我有一个 HashMap

{
"car01":["car", "coche", "macchina", "automobil"],
"dog01": ["dog", "perro", "cane", "cao"]
}

那么文件可能类似于

car01[car,coche,macchina,automobil]dog01[dog,perro,cane,cao]

索引 HashMap 可能类似于

{"car01":0, "dog01":35}

我尝试像这样迭代:

long characterCount = 0;

    HashMap<String, List<String>> index = indexOfIndexes.get(indexName);
    Path path = Paths.get(outputfile);
    try(Writer writer = Files.newBufferedWriter(path)) {
        index.forEach((key, value) -> {
            try { 
                writer.write(key + value);
            }
            catch (IOException ex) { throw new UncheckedIOException(ex); }
        });
    } catch(UncheckedIOException ex) { throw ex.getCause(); }

但我不知道如何计算每次有效使用的字符/字节数。

最佳答案

我认为你可以使用String的getBytes函数来计算序列化长度。 像这样:

long characterCount = 0;

HashMap<String, List<String>> index = indexOfIndexes.get(indexName);
Map<String, Long> count= new HashMap<>();
Path path = Paths.get(outputfile);
try(Writer writer = Files.newBufferedWriter(path)) {
    index.forEach((key, value) -> {
        try {
            count.put(key, characterCount);
            writer.write(key + value);
            characterCount= characterCount+  (key+ value).getBytes().length;
        }
        catch (IOException ex) { throw new UncheckedIOException(ex); }
    });
} catch(UncheckedIOException ex) { throw ex.getCause(); }

关于java - 如何在文件中序列化/保存非常大的 HashMap ,并知道文件中每个键的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58919955/

相关文章:

java - CustomAdapter 不显示列表中的 arraylist 项目

ruby - 如果键存在,向散列值添加一些东西?

java - 我没有收到 Oreo 和 upper 通知

java - 从 Eclipse 内部运行的 RCP 应用程序的 .log 文件在哪里?

java - 如何在Android 6.0中写入SD卡而不询问运行时权限?

java - 序列化 ENUM 单例

android - Parcelable 遇到 IOException 写入可序列化对象 getactivity()

android - Jsoup 输出的 HashMap

java - 在 Java 中声明数组及其名称的不同方式

不需要无参数构造函数和 Serializable 实现的 Java 序列化库