Name filtering in Java I have a search box for searching for people in a list of names.
Here is the code that runs every time the query changes.  I could not find anything open-sourced for this, so I wanted to make sure this looks good and efficient.

How does it look?

private final List&lt;String&gt; mTotalNames = ............ // some ArrayList&lt;String&gt; with all the names
private final List&lt;String&gt; mAvailableNames = new ArrayList&lt;String&gt;(mTotalNames);

private void doOnQueryTextChange(final String s) {
    mAvailableNames.clear();
    if (s.length() &lt;= 0) {
        mAvailableNames.addAll(mTotalNames);
    } else {
        final String query = s.toLowerCase();
        for (String name : mTotalNames) {
            name = name.toLowerCase();
            final List&lt;String&gt; partNameList
                    = new LinkedList&lt;String&gt;(Arrays.asList(name.split("\\s+")));
            if (helper0(query, partNameList)) {
                mAvailableNames.add(user);
            }
        }
    }
    // yeah, out mAvailableNames is all updated
}

private static boolean helper0(final String query, final List&lt;String&gt; partNameList) {
    for (String partQuery : query.split("\\s+")) {
        if (!helper1(partQuery, partNameList)) {
            return false;
        }
    }
    return true;
}

private static boolean helper1(final String partQuery, final List&lt;String&gt; partNameList) {
    if (partQuery.trim().length() &lt;= 0) return true;
    final Iterator&lt;String&gt; iterator = partNameList.iterator();
    while (iterator.hasNext()) {
        final String partName = iterator.next();
        if (partName.startsWith(partQuery)
                || partName.trim().length() &lt;= 0) {
            iterator.remove();
            return true;
        }
    }
    return false;
}
Note: The concepts presented in this answer have been implemented in the following 'alternate' implementation in this question: Name Filtering using a Trie


Interesting problem. Your code is hard to read, not because it is unstructured, or messy, but because the helper function names are hard to grasp.... what do they do?

So, on the good side, your 'visual' style is consistent, and neat. All your lines are indented well, and braced, etc. Well, except for this line:

if (partQuery.trim().length() &lt;= 0) return true;


Naming

The first issue is naming..... Four things:


helper0 and helper1 .... do not help, despite the name.
Not a big deal, it's probably a 'local rule', but it is unconventional to use 'prefixes' in Java for variable names. What does the m mean? mTotalNames should just be totalNames. You are using "Hungarian" notation of some sort, and that is not good practice in Java
'Part' is overloaded in too many places. partQuery, partName, partNameList, it becomes hard to remember which variables are which.
iterator is a horrible name. We know it is an iterator, but what does it iterate?


Algorithm

This is a situation where the right data structure will help a lot. You are doing and repeating too much work. Consider this:


you convert names to lower case often.
you split names.... often.
you get prefixes... often.


What you need is a better algorithm, and the right one, is to build a trie.

The trie will be indexed by the characters in the names. This will allow you to search for names in a very fast way, and not do any of the overhead processing repeatedly.
Build the trie once, and then use it repeatedly when you type to filter the names.

Bug

Your code will not process things correctly for a certain subset of names/queries.

Consider the name "Andy Anderson", and the search query "And Andy".... this will not match, because the search query word 'And' will match against 'Andy' first, and then that leaves the search term 'Andy', which will not match against the word 'Anderson'.

You need to sort the search terms in decreasing-length order, and always search with the longest query term first.

Follow Up

This subject intrigues me, I may implement my recommended solution myself... in a day or so.