Implementing a sort of key-value pair (not essentially a HashMap) in Java
What is the best way to implement the following scenario in Java:
A sort of key-value pair开发者_如何学运维 mechanism, but it looks like this:
param1 + param2 + param3 -> output1
param1 + * + !param3 -> output2
param1 + text containing value param2 + * -> output3
'*' => any parameter
!param => any value other than this parameter
text containing value 'param' => any data which contains the value 'param'. ex: aaaaa,bbb,param,ccc or aaaparambbb
In my view, a HashMap makes it difficult to implement this type of mapping. What is the best way to implement some mapping like this ?
I was also considering putting these in an Oracle table and trying to write a procedure, but there might be a better way in Java.A way of achieving this could be a triple nested hash-map. Or a hashmap of hashmaps of hashmaps.
The psuedo-code for querying would be something along the lines of this:
//You would call this method to search.
string query(string param1, string param2, string param3)
{
// The essence of the logic is, if you give a value for param3,
// then it will do the subquery of the hashMap that param3
// is the key of, if you don't supply a value (or provide the wildcard)
// it will search all the different hashmaps of the parent hashmap.
// See below for an example
if param1 != WILDCARD
then subquery1(hashmap[param1], string param2, string param3);
else for each x in hashmap, subquery1(x,string param2, string param3)
}
string subquery1(hashmap[hashmap[]] maps, string param2, string param3)
{
// The essence of the logic is, if you give a value for param2,
// then it will do the subquery of the hashMap that param2
// is the key of, if you don't supply a value (or provide the wildcard)
// it will search all the different hashmaps of the parent hashmap.
if param2 != WILDCARD
then subquery2(maps[param2], string param3);
else for each x in maps, subquery2(x, string param3)
}
string subquery2(hashmap[] maps, string param3)
{
if param3 != WILDCARD
then return maps[param3]
else for each x in maps, return maps[param3]
}
Obviously you need to define if multiple values are allowed to be returned and how you wish to resolve this. Also you need to determine if param 3 is nullable? The problem statement is pretty vague but I've done my best to answer what i think your problem is.
An example would be if you have added the following values to your hashmap.
key1, key2, key3 = value1
key1, key2, key4 = value2
If you searched key1, *, key3 you would get value1 returned.
If you searched key1, *, * you would get value1, and value2 returned.
Update:
When you call query("key1", "", "key3");
Since param1 is valid (not wildcard) we call subquery1(hashmap["key1"], "", "key3");
Before we get to subquery1 though, hashMap["key1"] is evaluated, but it returns another hashmap,
lets call this hashmap hashmap2[]. So subquery1 is actually called with (hashmap2[], "*", "key3");
Now we are in subquery1.
Since param2 is "*", we then iterate through all the values of hashmap2[],
for each hashmap3[] in hashmap2[], we call subquery3(hashmap3[], "key3");
At this point since param3 is valid, we call hashmap3["key3"] and we get value1 returned;
It sounds like what you have/need is an NFA where your params are the symbols.
http://en.wikipedia.org/wiki/Nondeterministic_finite-state_machine
You should use a trie to represent the values. This will allow you to quick and easily navigate your structure and also quickly manage your any and not conditions. You can find one here.
精彩评论