Data structure to store and retrieve a mapping between one action and multiple verbs in Java
What type of data structure must I use to build a dictionary like structure where one type of action is mapped to multiple verbs?
Example: Action1 will be mapped to verb1, verb2,verb5 and Action2 will be mapped to verb3,verb4.
Each action can have a varied number of verbs attached to it. The linking between action and Verbs will be hard coded.
Also, I need to search for a verb inputted by the user in the verb list defined and displayed in the corresponding action name:
Example: If the user input is verb 4 the system must return Action2
You can use a HashMap for this, the key would be the action and value would be the list of verbs:
Map<String,List<String>> actionVerbMap=new HashMap<>();
That sound like a Map of action and verb set
Use set to store verbs ensures the uniqueness and give good performance when you want to find action by given verb
I think a
HashMap<String, Action>will be suitable.
Since you want to use the verbs to access the actions but not vice versa, the verbs should be the keys.
Here are some example mappings:
verb1 -> Action1 verb2 -> Action1 verb5 -> Action1 verb3 -> Action2 verb4 -> Action2
This way you can ensure that no verb refers to two actions because keys are unique.
Data structure depends on relationship between verbs and actions. What is your relationship between verbs and action? 1-N N-N or N-1?
If you want exact dictionary like data structure, go for Prefix Tree or Trie. If there are n Number of keys and m is the Length of the key then Trie has Time Complexity of O(m).
Trie is preferrable if there is some lexicographical order or similar prefixes in your verbs.
t / \ te ta / \ \ tea ten tar
If that is not the case then HashMap is suitable option as described in other answers.