I have a large amount of Key-Value parameters that map to a file path. Most have the following form
filepath : /some/path
param_name_1 => 1234
param_name_2 => qwerty
But others can contain wildcard characters
filepath : /other/path
param_name_1 => 123*4
param_name_2 => ab?12
Where the ? is a wildcard matching any one character and * is a wildcard matching 0+ characters.
My users can provide their own set of KV parameters which I have to match and return the mapped path.
Example: User provides
param_name_1 => 1234
param_name_2 => qwerty
Application returns /some/path
User provides
param_name_1 => 123asdqweqweqdqweq1231asdcase4
param_name_2 => abW12
Application returns /other/path
For all the mappings that don’t contain wildcards, I can compute hashCode() for my stored mapping and for the user-provided one and do a HashMap lookup which is extremely fast (3-4 parameters to match, 100000 mappings, in 0 milliseconds, it’s a hash after all).
For mappings that contain wildcards though, I’m kind of stuck with doing linear look-ups through a list of all mappings that contain wildcards. There are about 2000-5000 such mappings and each look-up takes just under 200 milliseconds and I need to speed it up.
Is there a way I can do a general look-up to match wildcards or some other matching technique that would combine all mappings?
If you use a
TreeMapinstead of aHashMap, you can do a prefix search to reduce the number of items you have to iterate through. Just grab the characters that appear before the*or?, and iterate through all keys that start with those characters. This won’t work, of course, if your search term starts with a wildcard.The other common approach to this problem is to use character
ngramsor some trie-based structure, but that’s a lot more complicated.