views:

21

answers:

1

I am writing something in java that will try to "autocomplete" what a user is typing. I have used NSPredicate on iPhone apps to do this. It was very easy and worked well. I am hoping that there is something similar in java, but not having much luck finding it.

If something does not already exist to do this in java, does anyone have any suggestions on the best approach? I'm thinking of maybe doing something like having a Map with the key being "A", "B", "C", ... and the value being a list of sorted data that starts with the corresponding letter in order to get more manageable size lists and then iterating that list to find matches as each letter is typed.

Any other suggestions would be appreciated.

Thanks

A: 

You have not provided enough information. Is the list, against which you are autocompleting (henseforth referred to as the "target list", a static list of values? If yes, look at String.startsWith() and maybe the Comparator interface for searching the list.

If the target list is the result of a query, then structure your query to use partial matching (in oracle, you would have a where clause of "like 'xxx%'" xxx being the partial value and % being the oracle token for "match anything").

dwb
It is a list retrieved from a database table, but I want this to be fast so I would want to avoid trips to the database and keep everything in memory. The table is fairly static, only updated once per day, so I would reload the target list at some scheduled time once per day. Is there a way to use String.startsWith or a comparator interface without having to possibly iterate through the entire list?
ViniVidiVici