Android : Search from Large Arraylist

前端 未结 4 1854
轮回少年
轮回少年 2020-12-03 09:18

I have a recordset of about 29,000 records. My Screen contains EditText Box for Search Criteria and Listview containing all 29,000 records.

By searching with the lis

4条回答
  •  星月不相逢
    2020-12-03 09:50

    I would insist to use Lambdaj Library which is mostly used in such cases where you want to restrict loops for sorting and filtering Collections.

    Here is a small example for using lambdaj for filtering ArrayList.

    ArrayList sortedArrayList = select(arrList, having(on(String.class),
                                                       Matchers.containsString("a");
    

    This will return a complete filtered ArrayList with which you want to populate your ListView.

    You can also filter Custom Classes - Java: What is the best way to filter a Collection?

    UPDATE:

    Above solution was case-sensitive so to work around you can add Multiple Matchers.

    Like this you can add Multiple Matchers,

    ArrayList sortedArrayList = select(arrList, having(on(String.class),
       (Matchers.anyOf(Matchers.containsString("a"),Matchers.containsString("A")))));
    

    UPDATE:

    Even better way is to use filter(Matcher matcher, T...array)

    Here is how you can do that,

    ArrayList sortedArrayList = filter(Matchers.anyOf(
               Matchers.containsString("a"),Matchers.containsString("A")), arrList);
    

    Also, if you are interested in using some of the methods/features of lambdaj, you can extract the source and get it working. I am adding the same for filter()

    You can just download hamcrest-all-1.0.jar(63 kb) and add below code to get the filter() working

    public static  List filter(Matcher matcher, Iterable iterable) {
        if (iterable == null)
            return new LinkedList();
        else{
            List collected = new LinkedList();
            Iterator iterator = iterable.iterator();
            if (iterator == null)
                return collected;
            while (iterator.hasNext()) {
                T item = iterator.next();
                if (matcher.matches(item))
                    collected.add(item);
            }
            return collected;
        }
    }
    

    So, you can just sort out the least from lambdaj source and integrate in your source.

提交回复
热议问题