我有一个大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/