[英]What is the most efficient solution for reading properties from a properties file by prefix?
What is the most efficient (in terms of time complexity) solution for reading properties from a properties file by prefix? 通过前缀从属性文件中读取属性的最有效(就时间复杂度而言)解决方案是什么?
For example, if the properties file looks like this - 例如,如果属性文件如下所示-
prefix1.prop1=val1
prefix1.prop2=val2
prefix2.prop3=val3
prefix2.prop4=val4
I am looking a for a method which when called like this - getPropertiesByPrefix("prefix1")
should return the following list : 我正在寻找一种方法,当这样调用时getPropertiesByPrefix("prefix1")
应该返回以下列表:
["prefix1.prop1", "prefix1.prop2"]
I can see 2 options - 我可以看到2个选项-
getPropertiesByPrefix
call, read all the properties one by one and get the props starting with prefix. 在每个getPropertiesByPrefix
调用上,一一读取所有属性,并获取以prefix开头的道具。 getPropertiesByPrefix
uses the trie. 读取属性一次并构建一个Trie,然后getPropertiesByPrefix
使用Trie。 The 2nd option seems to be the more efficient way to go. 第二个选项似乎是更有效的方法。 Are there any existing implementations/third-party libs for these? 是否有任何现有的实现/第三方库? Or a third option? 还是第三个选择?
The analysis are simple. 分析很简单。
Say you have properties
of n number of keys, then a loop that does key.startsWith(prefix)
would run in O(m*n)
time, m being the size of the prefix. 假设您具有n个键的properties
,那么执行key.startsWith(prefix)
的循环将在O(m*n)
时间中运行,m是前缀的大小。
On the other hand, if you build a Trie
, this could be reduced O(m)
which is clearly better. 另一方面,如果构建Trie
,则可以将O(m)
减小,这显然更好。
Apache Commons have some implementations, although if n is small here then I really won't bother and avoid adding complexity to my code and go with a simple loop. Apache Commons有一些实现,尽管如果n很小,那么我真的不会打扰并且避免增加代码的复杂性并进行简单的循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.