Shohan Ahmed Sijan
Shohan Ahmed Sijan

Reputation: 4531

Android SQLITE search VS ArrayList search for small amount of dataset

Suppose, In my app I have a sqlite table that can contain at most 20 row. Each row has 2 column(id, name). Where I frequently need to search by Id to get Name. For this frequent need I have two solution:

Solution 1: Get rows in a arraylist<model> and then find name from array.

Solution 2: Every time search on sqlite table.

Now please give your opinion which one is better? Remember again, I need this search in my recycleView item, so it call so frequently.

Thanks

Upvotes: 0

Views: 230

Answers (1)

Yaroslav Mytkalyk
Yaroslav Mytkalyk

Reputation: 17115

I don't really get what is your real intent, but if your task is to search by id often, I would use

LongSparseArray<String> idsToNames; // or LongSparseArray<Model>

Which will map primitive long to Strings in a more memory-efficient way than Map and will have a better performance than ArrayList when searching.

The advantage over querying SQLite here is that you can do it in a blocking manner instead of having to query database on a background thread every time the lookup runs.

The disadvantage is that whenever data changes in SQLite, you will have to rebuild your idsToNames map. Also, if the number of entries in SQLite will eventually grow, you will end up in a large collection. So I would recommend this approach only if the updates to the database during this session will not happen, and if the data size is always predictable or fixed.

Upvotes: 2

Related Questions