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 :
["prefix1.prop1", "prefix1.prop2"]
I can see 2 options -
getPropertiesByPrefix
call, read all the properties one by one and get the props starting with prefix. getPropertiesByPrefix
uses the 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.
On the other hand, if you build a Trie
, this could be reduced O(m)
which is clearly better.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.