简体   繁体   English

在 Android 中保存 Map 的最佳和最快方法是什么?

[英]What's the best and fastest way to save Map in Android?

I need to save Map<String, LinkedHashMap<String[], Integer>>.我需要保存 Map<String, LinkedHashMap<String[], Integer>>。 I try to convert string to map and back using JSON and then I add map to SharedPreferences.我尝试使用 JSON 将字符串转换为 map 并返回,然后将 map 添加到 SharedPreferences。 The second way is Java serialization.第二种方式是Java序列化。 And the another one which I heard but have not use yet is a PaperDB (Github Link Of PaperDB: https://github.com/pilgr/Paper ).另一个我听说但尚未使用的是 PaperDB(PaperDB 的 Github 链接: https://github.com/pilgr/Paper )。 So, tell me what's the best way to save Map.那么,告诉我保存 Map 的最佳方法是什么。 And if you have other suggestions how to do that which I do not said about share with me(if that is better way).如果您有其他建议如何做我没有说的与我分享(如果那是更好的方法)。

The best and fastest way is by using Serializable interface:最好和最快的方法是使用 Serializable 接口:

final Map<String, LinkedHashMap<String[], Integer>> mapObject = new HashMap<>();
...
FileOutputStream fos = null;
try {
    fos = context.openFileOutput("/...path...", Context.MODE_PRIVATE);
    ObjectOutputStream os = new ObjectOutputStream(fos);
    os.writeObject(mapObject);
} catch (IOException e) {
    Log.e(....);
} finally {
    if (fos != null) {
        try { fos.close(); } catch (Exception ignored) { /*nothing*/ }
    }
}

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

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