I would insist on using the Lambdaj Library, which is mainly used in cases where you want to limit the loops for sorting and filtering the Collection.
Here is a small example of using lambdaj to filter an ArrayList .
ArrayList<String> sortedArrayList = select(arrList, having(on(String.class), Matchers.containsString("a");
This will return the full 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:
The above solution was case-sensitive , so for work you can add some matches .
Here you can add Multiple Matchers ,
ArrayList<String> sortedArrayList = select(arrList, having(on(String.class), (Matchers.anyOf(Matchers.containsString("a"),Matchers.containsString("A")))));
UPDATE:
Better yet, use filter(Matcher<?> matcher, T...array)
Here is how you can do it,
ArrayList<String> sortedArrayList = filter(Matchers.anyOf( Matchers.containsString("a"),Matchers.containsString("A")), arrList);
Also, if you are interested in using some lambdaj methods / functions, you can extract the source and make it work. I add the same for filter()
You can simply download hamcrest-all-1.0.jar(63 kb) and add the code below to get filter() working
public static <T> List<T> filter(Matcher<?> matcher, Iterable<T> iterable) { if (iterable == null) return new LinkedList<T>(); else{ List<T> collected = new LinkedList<T>(); Iterator<T> 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 simply determine the smallest of the lambdaj sources and integrate into your source.