0

I'm building a simple multi key hash map in a Java based application which would return a lookup value provided different combinations of keys where all keys and values are vanilla strings. Let's say below is a sample data set.

Key1|Key2|Key3|Key4|Result|
T1  | T2 | T3 | T4 | A1 |
*   |  * | T3 | T4 | A4 |
T1  | T2 | T3 | *  | A2 |
*   | T1 | *  | T4 | A2 |

where * indicates ANY value.

The hash map will comprise of keys 1-4 and result as it's look up value. The keys will have specific values(such as T1,T2) and it's only the data set that has *(ANY) values. I'm trying to figure out what could be a best possible way to look up the correct value based on the most specific key.

For example a key combination of T1,T2,T3,T4 (from above) should return A1 as the result whereas a key combination of B1,B2,T3,T4 should return A4 as the result.

Any ideas would be really appreciated. The preference is to do it in simple Java without any additional libraries/frameworks but happy to look at them if need be.

Thanks a lot

  • Can't you just use one good-old hashmap? – cantSleepNow May 10 '16 at 12:04
  • From the performance point of view, *trie* would be the best to implement such structure. A simpler option would be a `HashMap` where key is `Key1` (with a special value for `*`), and value is `List` of all nodes having given `Key1`. But there you'll need to iterate over all candidates to find the most specific. – Alex Salauyou May 10 '16 at 12:05
  • Thanks for the suggestions I will look at trie and see if it fits my purpose. – dacoder May 10 '16 at 12:42

0 Answers0