简体   繁体   中英

What is the fastest way to filter data from ArrayList?

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:

    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<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:

//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

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..

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM