0

I need to parse a document that have about 30k key value pairs of data for my android app. What is the best(fastest, or simpliest) way to store those key values? is it hashtable?, android sharedpreferences? or android sqlite?

thanks for reading and would appreciate any suggestions. ^^

xiaowoo
  • 2,248
  • 7
  • 34
  • 45

3 Answers3

0

If you don't want all of them in memory at same time, I would go with sqllite (not sure about to fastest, but atleast reduce runtime memory consumption, which is very imp factor because android restricts available memory for each process/app).

kosa
  • 65,990
  • 13
  • 130
  • 167
  • i would need to be able to access them quickly too. i think sqlite would make it slow because each time when i access it, i would need to make a query. – xiaowoo Nov 05 '12 at 20:32
  • I guess the slow would be negligible, again, its trade-off. I would suggest try and test before just deciding on its going to be slow. – kosa Nov 05 '12 at 20:33
0

You can use SharedPreferences. You can also just write a Map to file through serialization, so once input file is loaded into a HashMap, you can do something like this:

    FileOutputStream fStream = openFileOutput("saved-map", Context.MODE_PRIVATE) ;
    ObjectOutputStream oStream = new ObjectOutputStream(fStream);

    oStream.writeObject(map);        
    oStream.flush();
    oStream.close();

SQLLite is always a great option too, but the two above are fast and easy to implement.

Phil
  • 35,852
  • 23
  • 123
  • 164
  • Phil can you please explain a little on serialization?, i am not too familiar with it. thanks – xiaowoo Nov 05 '12 at 20:59
  • @xiaowoo, serialization is where you write an object as bits to a file. In Java, simple types (Strings, numbers, etc), as well as Collections of simple types can be saved this way. It is then treated just like you saved a file, and will persist across sessions. Here is a good example use: http://stackoverflow.com/questions/4738162/java-writting-reading-a-map-from-disk – Phil Nov 05 '12 at 21:17
0

There are some extreme differences between these storage types. And it all depends on your use case which one you use.

SharedPreferences is most likely a bad call here. Managing all those keys and being unable to iterate them in your preferences file is largely undesirable.

A hashtable only stores the data in memory, and will be lost whenever Android terminates the app or frees its memory. Additionally, this is a huge memory burden, dependent on what kind of data you're storing. (An int, int map may not be so bad.)

Most likely the best way to do this is via SQLite in a background thread. As I said, it depends on your use case, but you will not be able to manage 30k keys without either a speed loss or excessive memory use. That's just the restriction on today's technology.

Cat
  • 66,919
  • 24
  • 133
  • 141
  • i am most likely storing (string, string) key value pairs. and i agree with most of you guys that sqlite is better options for storing large amount of data but unfortunately in my case, i might have to make a lot of queries to retrive those data each time. each time when i need those data, i need to make about 500-1000 queries, which i am sure if querying from sqlite, it would be very very slow. – xiaowoo Nov 05 '12 at 20:41
  • @xiaowoo I think you need to consider optimizing the data, then. 500 queries is **a lot**, and you should really never need to do this many queries in a short period of time. For example, instead of `SELECT` statements for IDs 0-10, you can do `SELECT ... WHERE id >= '0' AND id <= '10'`. Your success with this data is going to be all about how you handle it, not necessarily about what medium you use to store it. – Cat Nov 05 '12 at 20:43