Peter
Peter

Reputation: 5131

Most efficient way to store and search through a large number of strings

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?

Upvotes: 1

Views: 1740

Answers (3)

AlexR
AlexR

Reputation: 115388

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.

Upvotes: 1

Milos Cuculovic
Milos Cuculovic

Reputation: 20223

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

Upvotes: 1

jbx
jbx

Reputation: 22178

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.

Upvotes: 1

Related Questions