简体   繁体   中英

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

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.

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? 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. Writing the xml is pretty easy. 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. Then write that string to disk. To get it back, use Gson again. Prob could accomplish this 5 lines of code. No messing with convertors or parsing needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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