高效过滤器的Java / Android的一个ArrayList [英] Efficiently filter an ArrayList in Java/Android
问题描述
我开发一个Android应用程序(安卓1.6),但是这可能是一个更一般的Java问题。
我有大约10,000个对象的ArrayList
对象包含3串(名字,middleName,姓氏)。
用户是psented一个搜索框在Android上,他们可以通过输入部分名称搜索特定的对象$ P $。
我有一个类(我称之为Filterer),搜索通过10000进行匹配对象的列表,然后返回它们作为一个子表。
搜索是一个有点慢(尤其是在Android手机),我敢肯定,我不这样做搜索/过滤以最有效的方式。
有没有人对如何加快我的搜索有什么建议?我的code是如下。一种可能性,以寻找对二级masterList已经有每一条信息在小写和级联...但可能会有其他的方法来改善这一搜索将有所帮助。
TIA !!
公共无效filterNames(){
this.filteredList.clear();
。串SV = this.searchString.toString.trim()与toLowerCase(); //搜索值
的for(int i = 0; I< this.masterList.size();我++){
为MyObject D = this.masterList.get(ⅰ);
字符串FN = d.getFirstName()的toString()与toLowerCase()。
。字符串MN = d.getMiddleName()的toString()与toLowerCase()。
。字符串LN = d.getLastName()的toString()与toLowerCase()。
如果(fn.indexOf(SV)> = 0 ||
md.indexOf(SV)> = 0 ||
ln.indexOf(SV)> = 0){
this.currentList.add(四);
}
}
}
是的,它肯定是痛苦的小写多个对象的每个循环迭代(外加一个可能是多余的的toString
),并且还不好的做法,叫则为list.size()
每次迭代&mdash?;该值应在循环开始之前被缓存。
无论如何,如果你正在使用这么多数据,有没有你不使用的SQLite数据库用于存储和显示/过滤使用列表的 的CursorAdapter
?
这将是推荐的方式来实现一些这样规模的。
I'm developing an Android app (Android 1.6), but this is probably a more general Java question.
I have an ArrayList of about 10,000 objects
the objects contain 3 strings (firstName, middleName, lastName).
The user is presented with a "search box" on android where they can search for a particular "object" by typing in part of the name.
I have a class (which I call Filterer) that searches through the list of 10,000 for matching objects and then returns them as a "sublist".
The search is a little bit SLOW (especially on an Android handset) and I'm sure I'm not doing the search/filtering in the most efficient manner possible.
Does anyone have any suggestions on how to speed up my search? My code is below. One possibility to to search against a secondary "masterList" that already has every piece of information in lowercase and concatenated…but there may be additional ways to improve this search that would also help.
TIA!!
public void filterNames() {
this.filteredList.clear();
String sv = this.searchString.toString.trim().toLowerCase(); // search value
for (int i = 0; i < this.masterList.size(); i++) {
MyObject d = this.masterList.get(i);
String fn = d.getFirstName().toString().toLowerCase();
String mn = d.getMiddleName().toString().toLowerCase();
String ln = d.getLastName().toString().toLowerCase();
if (fn.indexOf(sv) >= 0 ||
md.indexOf(sv) >= 0 ||
ln.indexOf(sv) >= 0) {
this.currentList.add(d);
}
}
}
Yes, it's certainly painful to lower-case several objects for each loop iteration (plus a possibly redundant toString
?), and also bad practice to call list.size()
for every iteration — that value should be cached before the loop starts.
Anyway, if you're working with this much data, is there a reason that you're not using an SQLite database for storage and displaying/filtering your list using CursorAdapter
?
That would be the recommended way to implement something of this size.
这篇关于高效过滤器的Java / Android的一个ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!