What is the fastest way to filter data from an ArrayList?

How can I filter data from an ArrayList? for example, I have one class called "Date Names". I wrote a little from the 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 will write to the database as follows:

 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 in an ArrayList as follows:

 ArrayList<DateAndNames> list = new ArrayList<DateAndNames>(); list = hand.getData(); 

and before I pass the list to the BaseAdapter, I want to filter it, so now I do the following:

 //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: when I have very very big data to run in a for loop, like 300 lines for filtering, the application is very slow! even if you use asyncTask, it is still slow! i'm a little newbie but i would like some good advice

Edited: I also tried this.

  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.

+6
source share
1 answer

I did not test which one is faster, either asking the database to return the filtered list, or doing it myself using a loop, because you can use several threads to cycle through the list, for example, consider using ExecutorService . Instead of sorting from 1 to 3000 lines in one stream, break it into several groups, each of which has, for example, 500 lines. Then pass each 500 lines to another runnable class and run all of them on the ExecutorService . Thus, the filtering time is divided by the number of processor cores. Another way is to set the index to the required columns and query the database with your parameters. As far as I know, the fastest way that you can achieve is one of the above methods, you can experiment and find the best.

+1
source

Source: https://habr.com/ru/post/980379/


All Articles