简体   繁体   English

Android-如何将具有自定义键(和自定义数据)的HashMap存储到内部存储中?

[英]Android - How to Store HashMap with Custom Key (and custom data) to internal storage?

I was wondering if it was possible to store a hash map with a special key function (similar to the solution posted by Jon Skeet at Using a byte array as Map key ) and thus data wrapper inside the android internal storage 我想知道是否可以使用特殊键功能存储哈希映射(类似于Jon Skeet在“ 使用字节数组作为Map键 ”中发布的解决方案),以及因此将数据包装器存储在android内部存储器中

And how to get them out again. 以及如何让他们再次离开。

Namely, the data underlying it all is char [], but that char [] is wrapped around in this custom class that is used in the hashmap. 即,所有基础数​​据都是char [],但是char []包裹在哈希映射中使用的此自定义类中。

The value part is simple string, but the key is the important bit where I need the data inside it to be preserved on each opening of the app. 值部分是简单的字符串,但是关键是重要的一点,我需要将其中的数据保存在应用程序的每次打开时。

Do I need to overwrite certain functions in the wrapper to make sure it works with the FileOutputStream? 我是否需要覆盖包装程序中的某些函数以确保其与FileOutputStream一起使用? How do I import it back again? 如何再次将其导入?

I don't have a direct answer for you. 我没有你的直接答案。 However I can suggest simply saving your hashmap as an xml file. 但是我建议您将哈希表另存为xml文件。 Writing the xml is pretty easy. 编写xml非常简单。 You will need to write the higher level parts of parsing to read it back. 您将需要编写解析的高级部分以将其读回。

Use Gson to convert the map to a JSON string. 使用Gson将地图转换为JSON字符串。 Then write that string to disk. 然后将该字符串写入磁盘。 To get it back, use Gson again. 要取回它,请再次使用Gson。 Prob could accomplish this 5 lines of code. 概率可以完成这5行代码。 No messing with convertors or parsing needed. 无需弄乱转换器或解析。

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

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