Java - 搜索提供前几个字符的字符串集合 [英] Java - search collection of strings providing the first few characters
问题描述
我有一个字符串集合,我想搜索只提供前几个字符。
I have a collection of strings that I want to search providing only the first few characters.
例如,考虑字符串列表:[tom,tomaz,爱丽丝,托尔斯泰,约翰]。字符串[to]将导致列表[tom,tomaz,tolstoy]。
For example, consider the list of strings: [tom, tomaz, alice, tolstoy, john]. The string [to] would result in the list [tom, tomaz, tolstoy].
性能非常大。
最佳化方法是什么?索引?排序?如何?
What is the best way to optimize this? Indexes? Sorting? How?
谢谢!
推荐答案
trie是通用解决方案,如果你想要一个没有外部依赖的轻量级和相对快的解决方案,只需将所有的字符串放入 TreeSet
并使用 tailSet()
找到与前缀匹配的第一个元素,然后遍历尾部集合,直到找到不匹配的字符串。 (注意:如果没有任何字符串与前缀匹配,这甚至可以是第一个元素。)
A trie is the universal solution, as has already been suggested but if you want a lightweight and relatively fast solution with no outside dependencies, simply put all your string into a TreeSet
and use tailSet()
to find the first element matching the prefix, then iterate through the tail set until you find a string that doesn't match. (Note: this could even be the first element if none of your strings match the prefix.)
如果你的列表不大于几千个字符串,方法在实践中是足够好的。
If your list isn't bigger than a couple of thousand strings, this method is good enough in practice.
这篇关于Java - 搜索提供前几个字符的字符串集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!