1

I have a large number of strings used in my Android application that are broken down into three categories: 1 word strings, 2 word strings, 3 word strings. I need to be able to search through these three lists of strings quickly looking for matches to a given key and returning a third value associated to these strings.

For instance if I'm looking for the key "New York" and my list is:

New York, test1
New Jersey, test2
Colorado, test3
Arkansas, test4
New York, test5

A search would return the strings: test1 and test5

What is the best way to store these strings that searching for matches is fastest on an android phone?

The options I thought of so far were: database, xml, csv. Are there any other options and what would be the best choice for this situation?

Edit: What if I wanted to preserve an order of the items so they could be moved up or down on the list, i know Hashmap is un-ordered. Would an arraylist work?

Peter
  • 5,071
  • 24
  • 79
  • 115
  • 3
    definitely skip xml. xml and speed do not generally get used in the same sentence in anything other than a negative sense. consider using the native sqlite facilities (e.g. database). – Marc B Nov 27 '12 at 16:43
  • Thanks I'll take a look into that, I don't have much sql or sqlite experience but I can learn – Peter Nov 27 '12 at 16:45
  • 1
    Regarding SQLite: There is a full-text-search extension that could be useful http://www.sqlite.org/fts3.html / http://stackoverflow.com/questions/6339022/sqlite3-fts4-match-and-android – zapl Nov 27 '12 at 16:48

3 Answers3

1

Why don't you store them as a Map between key and list of values, and use the inbuilt SQLLite database within Android to persist them permanently there?

So essentially, instead of your list, you would have:

New York: [test1, test5]
New Jersey: test2
Colorado: test3
Arkansas: test4

Keeping New York unique, you will get the results much faster. In traditional DB sense, it will be the foreign key to your values.

jbx
  • 21,365
  • 18
  • 90
  • 144
1

The best and fastes way is to use a database. Use SQLite to store and seearch data in your database.

See: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265
1

Parse your file, split each line using , as a delimiter. Create hash table (one of implentations of java.util.Map) and put the parsed results there, so that New York is a key and test1, test5 etc are values stored in list:

Map<String, Collection<String>> map = new HashMap<>();

Now you have direct access to this table and can always find mapping of any city.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • When you say parse my file, do you mean that I should store the lists of strings in say a csv file, and then every time I start up the application on my phone load the lists from the files into the hash table? Couldn't that take a large amount of time every time the app is launched? – Peter Nov 27 '12 at 17:16