使用Java中的通配符返回字符串列表的最快方法 [英] Quickest way to return list of Strings by using wildcard from collection in Java

查看:626
本文介绍了使用Java中的通配符返回字符串列表的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了100000字符串。例如,我想得到所有的字符串从JO开始。那么最好的解决方案是什么?



我在想 Aho-Corasick但是我的实现不支持通配符。

p>如果你希望所有的字符串以一个序列开始,你可以将所有的字符串添加到一个NavigableSet像TreeSet,并得到 subSet(text,text +'\\\￿')将提供以 text开头的所有条目这个查找是O(log n)






如果你想让所有的字符串以一个序列结尾,你可以做一个类似的事情,除非你必须反转String。在这种情况下,从反向String到正向String的TreeMap将是一个更好的结构。



如果你想要x * z,你可以搜索第一个集合,

如果您想要包含 x ,您可以使用导航< String,Set< String >>其中键是每个String从第一个,第二个,第三个char等开始的值该值是一个Set,因为你可以得到重复。你可以像结构的开始一样进行搜索。


I have set of 100000 String. And for example I want to get all strings starting with "JO" from that set. What would be the best solution for that?

I was thinking Aho-Corasick but the implementation I have does not support wild cards.

解决方案

If you want all the strings starting with a sequence you can add all the String into a NavigableSet like TreeSet and get the subSet(text, text+'\uFFFF') will give you all the entries starting with text This lookup is O(log n)


If you want all the Strings with end with a sequence, you can do a similar thing, except you have to reverse the String. In this case a TreeMap from reversed String to forward String would be a better structure.

If you want "x*z" you can do a search with the first set and take a union with the values of the Map.

if you want contains "x", you can use a Navigable<String, Set<String>> where the key is each String starting from the first, second, third char etc The value is a Set as you can get duplicates. You can do a search like the starts with structure.

这篇关于使用Java中的通配符返回字符串列表的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆