简体   繁体   English

从ArrayList过滤数据的最快方法是什么?

[英]What is the fastest way to filter data from ArrayList?

How can i filter data from ArrayList? 如何从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:

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! 现在,最大的问题是:当我有一个非常非常大的数据在for循环上运行时就像300行过滤一样,应用程序运行速度非常慢! even if using asyncTask it still working slow! 即使使用asyncTask它仍然工作缓慢! 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 . 我没有测试哪个是最快的要么DB要返回已过滤的列表,要么自己使用循环来执行它,因为您可以使用多个线程循环遍历列表,例如考虑使用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. 不是在单个线程上从1到3000行循环,而是将它分成多个组,每个组具有例如500行。 Then pass each 500 rows to a different runnable class and run all of them on ExecutorService . 然后将每500行传递给不同的runnable类,并在ExecutorService上运行所有这些行。 In this way the time of filtering is divided by the number of cores of the cpu. 以这种方式,过滤时间除以cpu的核心数。 Another way is setting index on the desired columns and query the DB with your parameters. 另一种方法是在所需列上设置索引并使用您的参数查询DB。 As far as I know the fastest way you can achieve is one of the above approach, you can experiment and find the best. 据我所知,你能达到的最快方法就是上述方法之一,你可以试验并找到最好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Firestore获取数据的最快方法是什么? - What is the fastest way to get data from Firestore? 更改 ArrayList 中元素顺序的最快方法是什么? - What is the fastest way to change the order of elements in an ArrayList? 在 java 中用 null 填充 ArrayList 的最快方法是什么? - What is the fastest way to fill an ArrayList with null in java? 从嵌套ArrayList中删除元素的最有效,最快的方法是什么? - What would be the most efficient and fastest way to remove an element from a nested ArrayList? 读取/过滤文本文件的最快方法是什么 - What is the fastest way to read/filter a text file 将数据从应用程序(Java)导入临时表的最快方法是什么? - What is the fastest way to import data from application (Java) into temporary table? 从数据库中检索顺序数据的最快方法是什么? - What is the fastest way to retrieve sequential data from database? 将 memory 中的大量数据写入文件的最快方法是什么? - What is the fastest way to write a large amount of data from memory to a file? 从Java应用程序获取数据到Cassandra 2的最快方法是什么? - What is the fastest way to get data into Cassandra 2 from a Java application? 从在线托管的mysql数据库中检索数据的最快方法是什么? - What is the fastest way of retrieving data from mysql database hosted online?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM