Using hashtable or SQL Lite in my android application
Initially I use hashtable in my android application. The key point I love hashtable is it is able to store complex item such as: coordinate-->float[2], velocity-->float[2].
But from many samples, using SQLlite table in and开发者_运维问答roid seems more efficient but it may only store all the values in one row defined by rowID.
So can anyone enlighten me on this problem? using hash table or SQL lite is more feasible? Can hashtable keep the data if I exit the application?
Can hashtable keep the data if I exit the application?
No, of course no. A HashTable
instance will live while your app is running, but as soon as you close the app, it will be deleted. Now, put your hand in your heart and answer these questions:
- How much data do you have to save?
- How often do you use that data?
- How long should the data persist?
That said, let's talk about more ways to persist data:
- Shared Preferences if you want an easy way to persist the simple data structures, this is the way to go. In fact, shared preferences allows you to save data in a
key<->value
schema which is very similar to theHashTable
. - SQlite this method is really useful, though it's not necessary if you are not saving complex data structures. If you just want to save some values, using SQLite is like killing a fly with a cannon.
- There are more ways to persist data which I think are not applicable to what you want to do.
So, at first glance, I suggest you take a look at the examples of Shared Preferences, which seems to be ideal for your scenario.
SQLite is not more efficient than a hash table. You're really comparing two completely different ideas. SQLite is for persistent storage, a hash table is for volatile storage. What does your application need?
精彩评论