How can i filter data from ArrayList? for example,I have one class called "Date Names". i wrote little from code below for my explanation:
如何从ArrayList过滤数据?例如,我有一个名为“日期名称”的类。我从下面的代码中写了一些我的解释:
public class DateAndNames {
int day;
int month;
int year;
String name;
public DateAndNames(int day, int month, int year, String name) {
super();
this.day = day;
this.month = month;
this.year = year;
this.name = name;
}
public int getDay() {
return day;
}
...getters and setters...
and i populate to database like that:
我填充到这样的数据库:
DbHandler hand = new DbHandler(this);
hand.add(new DateAndNames(20, 3, 2008, "Jhon"));
hand.add(new DateAndNames(10, 3, 2008, "Jhon"));
hand.add(new DateAndNames(10, 2, 2004, "Jhon"));
hand.add(new DateAndNames(22, 3, 2008, "Jhon"));
and then i get the data to ArrayList like that:
然后我得到数据到ArrayList:
ArrayList<DateAndNames> list = new ArrayList<DateAndNames>();
list = hand.getData();
and before i passing the list to the BaseAdapter, i want to filter it so what i doing right now is that:
在我将列表传递给BaseAdapter之前,我想过滤它,所以我现在正在做的是:
//filter by month and year:
public ArrayList<DateAndNames> filterTheList(int month , int year){
//the data from the database
list = hand.getData();
//temp list to store the filtered list
ArrayList<DateAndNames> filteredList = new ArrayList<DateAndNames>();
for (int i = 0; i < list.size(); i++) {
//check:
if(list.get(i).getMonth() == month && list.get(i).getYear() == year){
DateAndNames data = new DateAndNames(
list.get(i).getDay(),
list.get(i).getMonth(),
list.get(i).getYear(),
list.get(i).getName());
//The data filtered:
filteredList.add(data);
}
}
return filteredList;
}
now, the big problem is: when i have a very very very big data to run on the for loop like 300 rows to filter, the app running very slow! even if using asyncTask it still working slow! i'm a little bit new but i would like for good advices
现在,最大的问题是:当我有一个非常非常大的数据在for循环上运行时就像300行过滤一样,应用程序运行速度非常慢!即使使用asyncTask它仍然工作缓慢!我有点新,但我想要好的建议
Edited: i tried this too..
编辑:我也试过这个..
public ArrayList<DateAndNames> getData(int month ,int year,String name){
open();
ArrayList<DateAndNames> list = new ArrayList<DateAndNames>();
Cursor c = myDb.query(TABLE_DAY, null, "name= ? and month = ? and year = ?", new String[] {name,month+"",year+""}, null, null, null);
while (c.moveToNext()) {
DateAndNames resultData = new DateAndNames(
c.getInt(0), //id
c.getString(1),//name
c.getInt(2), //month
c.getInt(3));//year
list.add(resultData);
}
close();
return list;
}
But still not working..
但仍然没有工作..
1 个解决方案
#1
1
I have not tested which one is fastest either asking the DB to return the filtered list or yourself do it using a loop because you can use multiple threads to looping through the list, for example consider using ExecutorService
. Instead of looping from 1 to 3000 rows on a single thread split it in multiple groups each of them having for example 500 rows. Then pass each 500 rows to a different runnable class and run all of them on ExecutorService
. In this way the time of filtering is divided by the number of cores of the cpu. Another way is setting index on the desired columns and query the DB with your parameters. As far as I know the fastest way you can achieve is one of the above approach, you can experiment and find the best.
我没有测试哪个是最快的要么DB要返回已过滤的列表,要么自己使用循环来执行它,因为您可以使用多个线程循环遍历列表,例如考虑使用ExecutorService。不是在单个线程上从1到3000行循环,而是将它分成多个组,每个组具有例如500行。然后将每500行传递给另一个runnable类,并在ExecutorService上运行所有这些行。以这种方式,过滤时间除以cpu的核心数。另一种方法是在所需列上设置索引并使用您的参数查询DB。据我所知,你能达到的最快方法就是上述方法之一,你可以试验并找到最好的方法。
#1
1
I have not tested which one is fastest either asking the DB to return the filtered list or yourself do it using a loop because you can use multiple threads to looping through the list, for example consider using ExecutorService
. Instead of looping from 1 to 3000 rows on a single thread split it in multiple groups each of them having for example 500 rows. Then pass each 500 rows to a different runnable class and run all of them on ExecutorService
. In this way the time of filtering is divided by the number of cores of the cpu. Another way is setting index on the desired columns and query the DB with your parameters. As far as I know the fastest way you can achieve is one of the above approach, you can experiment and find the best.
我没有测试哪个是最快的要么DB要返回已过滤的列表,要么自己使用循环来执行它,因为您可以使用多个线程循环遍历列表,例如考虑使用ExecutorService。不是在单个线程上从1到3000行循环,而是将它分成多个组,每个组具有例如500行。然后将每500行传递给另一个runnable类,并在ExecutorService上运行所有这些行。以这种方式,过滤时间除以cpu的核心数。另一种方法是在所需列上设置索引并使用您的参数查询DB。据我所知,你能达到的最快方法就是上述方法之一,你可以试验并找到最好的方法。