问题描述
我正在用 Java 编写一些东西,它会尝试自动完成"用户输入的内容.我已经在 iPhone 应用程序上使用 NSPredicate 来做到这一点.这很容易,而且效果很好.我希望在 java 中有类似的东西,但没有太多运气找到它.
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.
如果在 Java 中尚不存在可以执行此操作的方法,是否有人对最佳方法有任何建议?我正在考虑做一些事情,比如拥有一个 Map ,其键是A"、B"、C"……,值是一个以相应字母开头的排序数据列表,以便获得更易于管理的大小列表,然后在输入每个字母时迭代该列表以查找匹配项.
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.
谢谢
推荐答案
您提供的信息不足.您正在自动完成的列表(以下称为目标列表",一个静态值列表?如果是,请查看 String.startsWith() 以及用于搜索列表的 Comparator 接口.
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.
如果目标列表是查询的结果,则构造您的查询以使用部分匹配(在 oracle 中,您将有一个 where 子句like 'xxx%'" xxx 是部分值,% 是 oracle匹配任何内容"的标记).
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").
这篇关于是否有相当于 NSPredicate 的 java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!